【发布时间】:2020-03-15 02:26:57
【问题描述】:
如何在 3 列 X_train 数据上使用以下代码。错误
“ValueError:查询数据维度必须匹配训练数据维度”
在使用 3 列 X_train 数据执行以下代码时发现。
# Visualising the Training set results
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('K-NN (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
【问题讨论】:
-
错误来自
classifier.predict,所以和numpy或者matplotlib没有关系。因此,请使用正确的标签并确保清楚classifier是什么。 -
分类器是knn分类器