from math import log
import operator
import pickle
def FileRead(path):#read the file
with open (path,'r') as f:
datatxt=f.readlines()
data=[]
for line in datatxt:
data.append(line.strip().split(','))
return data
def EntCalculate(dataset):
labels=[example[-1] for example in dataset]
labelClass=list(set(labels))
DataNum=len(labels)
ent=0
for a in labelClass:
Num=labels.count(a)
prob=Num/DataNum
ent-=prob*log(prob,2)
return ent
def SplitData(dataset,column,value):
SubData=[]
for featvec in dataset:
if featvec[column]==value:
SubValueList=featvec[:column]
SubValueList.extend(featvec[column+1:])
SubData.append(SubValueList)
return SubData
def ChooseAttribute(dataset):
AttrNum=len(dataset[0])-1 #num of attribute
DataNum=len(dataset)
BaseEnt=EntCalculate(dataset)
BestInfoGain=0
BestAttr=0
for i in rang
UnboundLocalError: local variable 'testlabel' referenced before assignment,为解决问题,python决策树
最新推荐文章于 2024-06-26 08:30:00 发布