0

Using sklearn SVC(), I am getting the below error

import sklearn

from sklearn.datasets import load_iris

iris = load_iris()

X, y = iris.data, iris.target

from sklearn.svm import SVC

# create the model
mySVC = SVC()

# fit the model to data
mySVC.fit(X,y)

# test the model on (new) data
result = mySVC.predict([3, 5, 4, 2])
print(result)
print(iris.target_names[result])

ValueError                                Traceback (most recent call last)
<ipython-input-47-8994407a09e3> in <module>()
      1 # test the model on (new) data
----> 2 result = mySVC.predict([3, 5, 4, 2])
      3 print(result)
      4 print(iris.target_names[result])

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/svm/base.py in predict(self, X)
    546             Class labels for samples in X.
    547         """
--> 548         y = super(BaseSVC, self).predict(X)
    549         return self.classes_.take(np.asarray(y, dtype=np.intp))
    550 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/svm/base.py in predict(self, X)
    306         y_pred : array, shape (n_samples,)
    307         """
--> 308         X = self._validate_for_predict(X)
    309         predict = self._sparse_predict if self._sparse else self._dense_predict
    310         return predict(X)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/svm/base.py in _validate_for_predict(self, X)
    437         check_is_fitted(self, 'support_')
    438 
--> 439         X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C")
    440         if self._sparse and not sp.isspmatrix(X):
    441             X = sp.csr_matrix(X)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    439                     "Reshape your data either using array.reshape(-1, 1) if "
    440                     "your data has a single feature or array.reshape(1, -1) "
--> 441                     "if it contains a single sample.".format(array))
    442             array = np.atleast_2d(array)
    443             # To ensure that array flags are maintained

ValueError: Expected 2D array, got 1D array instead:
array=[3. 5. 4. 2.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

1 Answer 1

2

As error mentioned, you will have to pass 2-D array. You can try using as following:

result = mySVC.predict([[3, 5, 4, 2]])

You need to pass samples, here each sample is an array, so what are you passing is just one sample (as one sample has 4 features) not samples. Note that you will receive array/list of predictions as well for each samples passed for prediction in order.

From documentation:

predict(X)

Perform classification on samples in X.

For an one-class model, +1 or -1 is returned.

Parameters:

X : {array-like, sparse matrix}, shape (n_samples, n_features) For kernel=”precomputed”, the expected shape of X is [n_samples_test, n_samples_train]

Returns:

y_pred : array, shape (n_samples,)

Class labels for samples in X.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.