『機械学習実戦』ノート:TypeError:unsupported operand type(s)for*:'float'and'NoneType'

1141 ワード

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]
        uniqueVals = set(featList)
        newEntropy = 0.0
        for value in uniqueVals:
            subDataSet = splitDataSet(dataSet, i, value)
            prob = len(subDataSet)/float(len(dataSet))
            newEntropy += prob*calcShannonEnt(subDataSet)
        infoGain = baseEntropy - newEntropy
        if(infoGain > bestInfoGain):
            bestInfoGain = infoGain
            bestFeature = i
    return bestFeature
Traceback (most recent call last):
  File "", line 1, in
    trees.chooseBestFeatureToSplit(myDat)
  File "E:\AI\FirstPythonProj\trees.py", line 52, in chooseBestFeatureToSplit
    newEntropy += prob*calcShannonEnt(subDataSet)
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
calcShannonEnt()関数が定義時に値を返さなかったため、関数中のreturn位置が間違っており、再修正後OK(Python 3.6.4)