IndexError:索引1超出轴0的大小为1的范围

时间:2017-03-09 05:09:03

标签: python numpy matplotlib

我正在关注一本名为“机器学习在行动”的书。我在第5章中提到了一个问题。当我跑步时:

def plotBestFit(wei):
    import matplotlib.pyplot as plt
    wei = np.mat(wei)
    weights = wei.getA()
    dataMat, labelMat = loadDataSet()
    dataArr = np.array(dataMat)
    n = np.shape(dataArr)[0]
    xcord1 = []; ycord1 = []
    xcord2 = []; ycord2 = []
    for i in range(n):
        if int(labelMat[i]) == 1:
            xcord1.append(dataArr[i,1]);ycord1.append(dataArr[i,2])
        else:
            xcord2.append(dataArr[i,1]);ycord2.append(dataArr[i,2])
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.scatter(xcord1, ycord1, s=30, c='red', marker = 's')
        ax.scatter(xcord2, ycord2, s=30, c='green')
        x = np.arange(-3.0, 3.0, 0.1)
        y = (-weights[0] - weights[1]*x)/weights[2]
        ax.plot(x,y)
        plt.xlabel('X1');plt.ylabel('X2');
        plt.show()

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\python2.7.12\lib\logRegres.py", line 59, in plotB
    y = (-weights[0] - weights[1]*x)/weights[2]
IndexError: index 1 is out of bounds for axis 0 with size 1

我怎样才能让它发挥作用?

0 个答案:

没有答案