-
大小: 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实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论