我正在学习python/ML,遇到了这些错误。我没有线索,因为密码是好的。在
编码
from sklearn import datasets
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
X, y = mglearn.datasets.make_forge()
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
line = np.linspace(-3, 3, 1000).reshape(-1, 1)
for n_neighbors, ax in zip ([1,3,9], axes):
reg = KNeighborsRegressor(n_neighbors=n_neighbors)
reg.fit(X_train, y_train)
ax.plot(line, reg.predict(line))
ax.plot(X_train, y_train, '^', c=mglearn.cm2(0), markersize=8)
ax.plot(X_test, y_test, 'v', c=mglearn.cm2(1), markersize=8)
ax1.set_title(
"{} neighour(s)\n train score: {:.2f} test score: {:.2f}".format(
n_neighbors, reg.score(X_train, y_train),
reg.score(X_test, y_test)))
ax.set_xlabel("feature")
ax.set_ylabel("target")
axes[0].legend(['model predictions', 'training data/target',
'test data/target'], loc='best')
错误
^{pr2}$我好像搞不清是什么错误,如有任何帮助我将不胜感激。在
正如其他人所说,X和line具有不同数量的特征。这是我书中的一个例子,完整的代码here。
将提供本书和我链接到的笔记本中使用的1d数据集。
你忘了导入Mglearn。它可以通过pip install Mglearn安装在Ubuntu中。之后, 导入mglearn 它会开始工作的,我也一样!!在
相关问题 更多 >
编程相关推荐