MGLEARN绘图KNN分类没有显示情节

时间:2017-11-24 22:40:07

标签: python plot machine-learning scikit-learn knn

我正在阅读这本书Introduction to Machine Learning的第一个ML步骤。 我正在尝试生成片段的图片" 在[10] "您可以在 this 页面上找到它,但它无法正常工作。当我说它不起作用时,我的意思是当我点击"运行" (既不是错误信息)。

以下代码有什么问题?

我认为我错过了像plt.show()代码这样的东西,但是在谷歌看来似乎mglearn不需要/拥有这种结构。

from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn.plots

X, y = mglearn.datasets.make_forge()
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.legend(["Class 0", "Class 1"], loc=4)
plt.xlabel("First feature")
plt.ylabel("Second feature")
print("X.shape: {}".format(X.shape))
#plt.show()


mglearn.plots.plot_knn_classification(n_neighbors=1)

我正在使用Python 3.6.3。

2 个答案:

答案 0 :(得分:1)

根据@Sascha提示,我得到以下工作代码:

from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn.plots

X, y = mglearn.datasets.make_forge()


mglearn.plots.plot_knn_classification(n_neighbors=1)
plt.show()

答案 1 :(得分:1)

使用显示函数比使用 plt.show() 更好。

import mglearn
from IPython.display import display

X,y=mglearn.datasets.make_forge()
knn=mglearn.plots.plot_knn_classification(n_neighbors=1)
display(knn)

然后,我们将拥有与书相同的图片。 enter image description here

如果使用plt.show(),图片会不一样。 enter image description here

相关问题