“ValueError: Expected 2D array, got 1D array instead” while predicting using SVC in python

“ValueError: Expected 2D array, got 1D array instead” while predicting using SVC in python



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



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.






thanks a ton... i figured it out too... :-)

– A. Sinha
Sep 17 '18 at 3:09



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)