python - 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
正确?
谢谢你。
解决方案
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)
尝试这个
推荐阅读
- sharepoint - C# 的图形 API:尝试在 Office 365 上创建团队时的 BadGateway 结果
- python - 安装有 Pip 问题的 `pygame`
- flutter - 如何在颤振中重新启动整个应用程序?
- node.js - 什么是好的通知模型?
- javascript - 将修改后的表发送到 ASP.NET MVC 中的控制器?
- python - 使用 Python 将 2 个视频连接成 1 个视频
- powershell - Fiddler 未在 PowerShell 中捕获某些命令
- c++ - 在函数“lasd::Vector
::~Vector()" 对基类的未定义引用 - javascript - 如何使用 js RegExp 删除空格
- python - 如何将“列表数据库”与 Python 链接?