91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

邏輯斯蒂回歸,用Python語言寫的,比較簡單,在小數據集上準確率為100%,資源中包含完整代碼及測試數據

資源截圖

代碼片段和文件信息

#Logistic.py
#2015-01-12
#Logistics終止條件w變化閾值對結果有著重大影響

import?logging
import?math

logging.basicConfig(level=logging.DEBUG
????????????????format=‘%(asctime)s?%(filename)s[line:%(lineno)d]?%(levelname)s?%(message)s‘
????????????????datefmt=‘%a?%d?%b?%Y?%H:%M:%S‘
????????????????filename=‘LosisticRegression.log‘
????????????????filemode=‘a+‘)

def?Sampling(FileName?TrainingSet?TestSet):
????fin?=?open(FileName?‘r‘)
????fout1?=?open(TrainingSet?‘w‘)
????fout2?=?open(TestSet?‘w‘)
????i?=?1
????for?x?in?fin:
????????if?0?==?i%3:
????????????fout2.write(x)
????????else:
????????????fout1.write(x)
????????i?=?i+1
????fin.close()
????fout1.close()
????fout2.close()

class?DataProcess(object):
????def?__init__(self?FileName):
????????self.__FileName?=?FileName
????????self.__Sample?=?[]
????????self.__IsLoaded?=?False

????def?LoadData(self?Separator?=?‘‘):
????????try:
????????????file?=?open(self.__FileName?‘r‘)
????????except?FileNotFoundError:
????????????logging.error(‘Can?not?find?file?%s‘?%?self.__FileName?)
????????for?x?in?file:
????????????self.__Sample.append(self.TypeParse(x.split(Separator)))
????????self.IsLoaded?=?True

????def?TypeParse(self?Item):
????????result?=?[]
????????for?x?in?Item:
????????????try:
????????????????result.append(float(x))
????????????except?ValueError:
????????????????if?x.endswith(‘\n‘):
????????????????????result.append(x.replace(‘\n‘?‘‘))
????????????????else:
????????????????????result.append(x)

????????return?result

????def?PreProcess(self?AttrID?=?[0]):
????????if?self.__IsLoaded?==?False:
????????????self.LoadData()
????????for?x?in?self.__Sample:
????????????for?y?in?AttrID:
????????????????x.pop(y)

????def?GetData(self):
????????return?self.__Sample

class?LogisticRegression(object):
????def?__init__(self?FileNameStepSize?=?0.5Epsilon?=?0.01):
????????dp?=?DataProcess(FileName)
????????dp.PreProcess([])
????????self.__StepSize?=?StepSize
????????self.__Epsilon?=?Epsilon
????????self.__Sample?=?dp.GetData()
????????self.__AttrNum?=?len(self.__Sample[0])
????????self.__Weight?=?[0.5]*(self.__AttrNum-1)
????????TempClassID?=?self.__GetClassID()
????????if?len(TempClassID)?!=?2:
????????????logging.error(‘Logistic?regression?is?just?for?two?classification‘)
????????????return
????????self.__ClassIDMap?=?{TempClassID[0]:1TempClassID[1]:-1}

#暫時沒啥用
????def?__Normalize(self):
????????for?i?in?range(0self.__AttrNum-1):
????????????Attr?=?[x[i]?for?x?in?self.__Sample]
????????????MaxVal?=?max(Attr)
????????????MinVal?=?min(Attr)
????????????for?j?in?range(0len(self.__Sample)):
????????????????self.__Sample[j][i]?=?(self.__Sample[j][i]-MinVal)/(MaxVal-MinVal)*2-1

????def?__GetClassID(self):
????????occure?=?[]
????????for?x?in?self.__Sample:
????????????if?x[self.__AttrNum-1]?not?in?occure:
????????????????occure.append(x[self.__AttrNum-1])
????????return?occure

????def?LRLeaner(self):
?????

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件???????4807??2015-01-16?16:11??Losisitc.py

?????文件???????1025??2015-01-12?13:10??Iris_TestSet.txt

?????文件???????2073??2015-01-12?13:10??Iris_TrainingSet.txt

-----------?---------??----------?-----??----

?????????????????7905????????????????????3


評論

共有 條評論