-
大小: 18KB文件类型: .rar金币: 2下载: 1 次发布日期: 2021-06-06
- 语言: Python
- 标签: 机器学习 Python Logistic regression
资源简介
机器学习LR分类器算法的Python实现,博文参考
http://blog.csdn.net/suipingsp/article/details/41822313

代码片段和文件信息
‘‘‘Created on Mar20 2014
Logistic regression classify with (Random) Gradient Ascent method
@author: Aidan
‘‘‘
from numpy import *
from object_json import *
from copy import *
import pdb
class logisticRegres(object):
def __init__(selfclassifierArray = None **args):
‘‘‘classifierArray is (1m+1)numpy array m is the
feture number of sample‘‘‘
obj_list = inspect.stack()[1][-2]
self.__name__ = obj_list[0].split(‘=‘)[0].strip()
self.classifierArray = classifierArray
def jsonDumpsTransfer(self):
‘‘‘essential transformation to Python basic type in order to
store as json. dumps as objectname.json if filename missed ‘‘‘
#pdb.set_trace()
self.classifierArray = self.classifierArray.tolist()
def jsonDumps(self filename=None):
‘‘‘dumps to json file‘‘‘
self.jsonDumpsTransfer()
if not filename:
jsonfile = self.__name__+‘.json‘
else: jsonfile = filename
objectDumps2File(self jsonfile)
def jsonloadTransfer(self):
‘‘‘essential transformation to object required type such as numpy matrix
call this function after newobject = objectLoadFromFile(jsonfile)‘‘‘
#pdb.set_trace()
self.classifierArray = array(self.classifierArray)
def getClassifierArray(self):
return self.classifierArray
def setClassifierArray(self classifierArray):
self.classifierArray = deepcopy(classifierArray)
def __sigmoid(self inX):
return 1.0/(1+exp(-inX))
def classifyArray(self dataToClassList):
‘‘‘dataToClassListis (1n)numpy array n indicates the sample number each sampe is (1m)list
for example [[12]][[12][34]]‘‘‘
dataToClassMat = mat(dataToClassList)
#pdb.set_trace()
nm = dataToClassMat.shape
estClassMat = self.__sigmoid(self.classifierArray * dataToClassMat.T)
#pdb.set_trace()
estClassMat[estClassMat>0.5] = 1.0
estClassMat[estClassMat<=0.5] = 0.0
return estClassMat
def classifySample(self dataToClass):
‘‘‘dataToClass is a sample. for example [12]‘‘‘
prob = self.__sigmoid(sum(self.classifierArray * dataToClass))
if prob > 0.5:
return 1
else:
return 0
def __gradAscent(self dataMatIn classLabelsnumIter=500):
‘‘‘the return weights is (1m+1)numpy array
m indicates the sample feturen number‘‘‘
dataMatrix = mat(dataMatIn) #convert to NumPy matrix
labelMat = mat(classLabels).transpose() #convert to NumPy matrix
mn = shape(dataMatrix)
alpha = 0.001#step
weights = mat(ones((n1)))#the default value is 1.0
for k in range(numIter): #heavy on matrix operations
h = self.__sigmoid(dataMatrix*weights) #matrix mult
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3788 2010-11-01 15:09 LR\horseColicTest.txt
文件 60655 2010-11-01 14:56 LR\horseColicTraining.txt
文件 6505 2014-12-09 11:12 LR\lr.py
文件 7268 2014-12-03 16:40 LR\lr.pyc
文件 543 2014-12-03 16:38 LR\LRClassifier150.json
文件 539 2014-12-03 16:41 LR\LRClassifier250.json
文件 538 2014-12-03 16:41 LR\LRClassifier300.json
文件 539 2014-12-03 16:41 LR\LRClassifier500.json
文件 2789 2014-12-01 12:11 LR\ob
文件 3920 2014-12-02 15:42 LR\ob
文件 2136 2014-12-09 11:12 LR\test.py
文件 2187 2010-10-29 06:46 LR\testSet.txt
目录 0 2014-12-03 16:41 LR
----------- --------- ---------- ----- ----
91407 13
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论