Python plt:索引器错误:数组的索引太多

Python plt:索引器错误:数组的索引太多,python,pandas,numpy,matplotlib,knn,Python,Pandas,Numpy,Matplotlib,Knn,我在此数据集上使用KNN分类器: 我正在尝试获取图形,但出现以下错误: Traceback (most recent call last): File "/home/ashutosh/Machine Learning A-Z Template Folder/Part 3 - Classification/Section 20 - Random Forest Classification/P14-Random-Forest-Classification/Random_Forest_Class

我在此数据集上使用KNN分类器:

我正在尝试获取图形,但出现以下错误:

Traceback (most recent call last):
  File "/home/ashutosh/Machine Learning A-Z Template Folder/Part 3 - Classification/Section 20 - Random Forest Classification/P14-Random-Forest-Classification/Random_Forest_Classification/rft.py", line 38, in <module>
    plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1], c=ListedColormap(('red', 'green'))(i), label = j)
IndexError: too many indices for array


您可以使用(单独)
print(x_set)
print(x_set[y_set==j,0])
等来查看哪个元素出了问题。在打印x_set时,它返回x_列的值,但在打印后一列时,它给出了一个名称错误,因为j不是defined@furas您还有其他建议吗始终放置完整的错误消息(从单词“Traceback”开始)(不是注释)作为文本(不是屏幕截图)。还有其他有用的信息。
j
for
-loop中定义,因此您必须在
for
-loop中使用它。
from matplotlib.colors import ListedColormap
x_set, y_set = x_train, y_train
x1, x2 = np.meshgrid(np.arange(start=x_set[:, 0].min() - 1, stop=x_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start=x_set[:, 1].min() - 1, stop=x_set[:, 1].max() + 1, step = 0.01))
plt.contour(x1, x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
            alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(x1.min(), x1.max())
plt.ylim(x2.min(), x2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1], c=ListedColormap(('red', 'green'))(i), label=j)
plt.show()