The place where my code reports an error is
uniqueVals = set(featList)
The system says:
'NoneType' object is not iterable
I try build it(uniqueVals) as a set firstly,but this has no use.
from math import log
def calEnt (dataset):
labelcount={}
for labels in dataset:
currentlabel=labels[-1]
if currentlabel not in labelcount.keys():
labelcount[currentlabel]=0
labelcount[currentlabel]+=1
Ent=0
for key in labelcount:
prob=labelcount[key]/len(dataset)
Ent+=prob*log(prob,2)
return -Ent
def splitfeature (featList, value):
splitresult=[]
for vote in featlist:
if vote[-1]==value:
splitresult.append(vote)
return splitresult
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0]) - 1
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0
bestFeature = -1
for i in range(numFeatures):
featList = [example[i] for example in dataSet]
Label=[example[-1]for example in dataSet]
featList=featList.append(Label)
uniqueVals = set(featList)
newEntropy=0
for value in uniqueVals:
subdataset = splitDataSet(featList, value)
prob = len(subdataset) / len(dataSet)
newEntropy += prob * calEnt((subdataset))
infoGain = baseEntropy - newEntropy
if (infoGain > bestInfoGain):
bestInfoGain = infoGain
bestFeature = i
return bestFeature