MGLEARN Plot KNN 分类不显示情节

MGLEARN Plot KNN classification doesn't show the plot

我正在阅读本书的第一个 ML 步骤 Introduction to Machine Learning。 我正在尝试生成您可以在 this 页面上找到的片段“In [10]”的图片,但是它不工作。当我说它不起作用时,我的意思是当我点击 "run" 时没有显示任何内容(也不是错误消息)。

下面的代码有什么问题?

我认为我缺少类似 plt.show() 的代码,但查看 Google 似乎 mglearn 没有 need/have 这种构造。

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.

根据@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()

使用显示功能比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