Sklearn ValueError:每个样本X具有2个功能;期待11

时间:2019-08-06 17:06:41

标签: python scikit-learn

我尝试可视化多个逻辑回归,但出现上述错误。

我正在练习kaggle的red wine quality数据集。

这里是完整的追溯:

EditText date_birthday = findViewById(R.id.date_birthday);
date_birthday.requestFocus();

下面是可视化代码:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-88-230199fd3a97> in <module>
      4 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
      5                      np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
----> 6 plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
      7              alpha = 0.75, cmap = ListedColormap(('red', 'green')))
      8 plt.xlim(X1.min(), X1.max())

/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/base.py in predict(self, X)
    287             Predicted class label per sample.
    288         """
--> 289         scores = self.decision_function(X)
    290         if len(scores.shape) == 1:
    291             indices = (scores > 0).astype(np.int)

/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/base.py in decision_function(self, X)
    268         if X.shape[1] != n_features:
    269             raise ValueError("X has %d features per sample; expecting %d"
--> 270                              % (X.shape[1], n_features))
    271 
    272         scores = safe_sparse_dot(X, self.coef_.T,

ValueError: X has 2 features per sample; expecting 11

1 个答案:

答案 0 :(得分:1)

您可以添加完整的代码来确定问题所在,但似乎该模型是使用11个特征训练的,但是现在您尝试使用2个特征进行预测。

classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape))

在这里,np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape)的形状在列尺寸(轴= 1)上应该与用于.fit的训练(classifier)的原始数组完全相同