Skip to content

ValueError: X has 2 features per sample; expecting 9 #1

Open
@jsitha

Description

@jsitha

1.Supervised Learning/2.Classification/5.Logistic Regression/Breast Cancer Coimbra Dataset (UCI) prediction.ipynb

Visualising the Test set results

from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
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, LG.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('Logistic Regression (Test set)')
plt.xlabel('Distance')
plt.ylabel('Cities')
plt.legend()
plt.show()


ValueError Traceback (most recent call last)
in
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, LG.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())

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in predict(self, X)
279 Predicted class label per sample.
280 """
--> 281 scores = self.decision_function(X)
282 if len(scores.shape) == 1:
283 indices = (scores > 0).astype(np.int)

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in decision_function(self, X)
260 if X.shape[1] != n_features:
261 raise ValueError("X has %d features per sample; expecting %d"
--> 262 % (X.shape[1], n_features))
263
264 scores = safe_sparse_dot(X, self.coef_.T,

ValueError: X has 2 features per sample; expecting 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions