《机器学习实战》第7章的一处代码错误

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-69-fab03f1fce4e> in <module>()
----> 1 adaboost.buildStump(dataArr, labelArr, D)

/home/cavin/Documents/BookData/machinelearninginaction/Ch07/adaboost.py in buildStump(dataArr, classLabels, D)
     44                 predictedVals = stumpClassify(dataMatrix, i, threshVal, inequal)
     45                 errArr = np.mat(np.ones((m, 1)))
---> 46                 errArr[predictedVals == labelMat] = 0
     47                 weightedError = D.T * errArr
     48                 print("""split: dim %d, thresh %.2f, thresh inequal: 

IndexError: index 1 is out of bounds for axis 1 with size 1

P120 程序清单7-1 单层决策树生成函数

def buildStump(dataArr, classLabels, D):
    ...
    errArr[predictedVals == labelMat] = 0

在3.6版本的anaconda下执行会提示错误:

解决方法:

errArr[predictedVals == labelMat.T] = 0

也就是把labelMat转置,出现这个错误原因是predictedValslabelMat的shape不同导致的

posted @ 2018-03-19 22:24  东围居士  阅读(565)  评论(2编辑  收藏  举报