《机器学习实战》笔记:TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

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(Python3.6.4)

你可能感兴趣的:(《机器学习实战》笔记:TypeError: unsupported operand type(s) for *: 'float' and 'NoneType')