0

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
desertnaut
  • 57,590
  • 26
  • 140
  • 166
ashely
  • 1
  • 1
  • Does this answer your question? [Why does append() always return None in Python?](https://stackoverflow.com/questions/16641119/why-does-append-always-return-none-in-python) – mkrieger1 Jan 25 '22 at 10:32

1 Answers1

0
featList=featList.append(Label)

Check out what is append return value.

matszwecja
  • 6,357
  • 2
  • 10
  • 17