-
大小: 18KB文件類型: .rar金幣: 2下載: 1 次發(fā)布日期: 2021-06-06
- 語(yǔ)言: Python
- 標(biāo)簽: 機(jī)器學(xué)習(xí)??Python??Logistic??regression??
資源簡(jiǎn)介
機(jī)器學(xué)習(xí)LR分類器算法的Python實(shí)現(xiàn),博文參考
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
????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????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
評(píng)論
共有 條評(píng)論