首页 > 解决方案 > plot_2d_separator 在使用 DataFrame 对象时抱怨(引发了 AttributeError)

问题描述

我作为 DataFrame 对象收集的数据点 X 很少。Y 类是根据 X 最后一列的值构造的 numpy 数组。我想可视化由 1、3、9 个最近邻模型创建的决策边界。我使用 X.values 能够对数组使用 Numpy 方法(例如切片)。

fig, axes = plt.subplots(1, 3, figsize=(10, 3))
XX = X.values

for n_neighbors, ax in zip([1, 3, 9], axes):
    clf = KNeighborsClassifier(n_neighbors=n_neighbors).fit(X, Y)
    mglearn.plots.plot_2d_separator(clf, XX, fill=True, eps=0.5, ax=ax, alpha=.4)
    mglearn.discrete_scatter(XX[:, 2], XX[:, 4], Y, ax=ax)
    ax.set_title("{} neighbor(s)".format(n_neighbors))
    ax.set_xlabel("nbpolys")
    ax.set_ylabel("GB time")

我收到以下错误:

decision_values = classifier.decision_function(X_grid)
AttributeError: 'KNeighborsClassifier' object has no attribute 'decision_function'

decision_function被调用plot_2d_separator.py

可能是什么问题呢?

传递的参数及其类型是否plot_2d_separator正确?

谢谢你。

标签: pythonpandasmachine-learningscikit-learn

解决方案


flg, axes = plt.subplots(1, 3, figsize=(10,3))

for n_neighbors, ax in zip([1,3,9], axes):
    clf = KNeighborsClassifier(n_neighbors).fit(X,y)
    mglearn.plots.plot_2d_separator(clf, X, fill=True, eps=0.5, ax=ax, alpha=0.4)
    mglearn.discrete_scatter(X[:,0], X[:,1], y, ax=ax)
    ax.set_title("{} neighbor(s)" .format(n_neighbors))
    ax.set_xlabel("feature 0")
    ax.set_ylabel("feature 1")
axes[0].legend(loc=3)

尝试这个


推荐阅读