索引器错误:索引1超出大小为1的轴0的界限

2024-05-15 16:52:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一本叫《机器学习在行动》的书。我在第五章遇到了一个问题。当我跑步时:

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()

It错误:

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

我怎样才能让它工作?在


Tags: infornppltaxweightsappendwei