Error in Python script “Expected 2D array, got 1D array instead:”?

229    Asked by ranjan_6399 in Python , Asked on Apr 10, 2021

 I'm following this tutorial to make this ML prediction:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn import svm
x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]
plt.scatter(x,y)
plt.show()
X = np.array([[1,2],
             [5,8],
             [1.5,1.8],
             [8,8],
             [1,0.6],
             [9,11]])
y = [0,1,0,1,0,1]
X.reshape(1, -1)
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)
print(clf.predict([0.58,0.76]))
I'm using Python 3.6 and getting value error "Expected 2D array, got 1D array instead:" I think the script is for older versions, but I don't know how to convert it to the 3.6 version.
Already try with the:
X.reshape(1, -1)
Answered by Ranjana Admin

 Pass 2D array instead of 1d array in the predict() function, to remove value error: expected 2d array, got 1d array instead

For example:
Replace
[0.58,0.76]
With
[[0.58,0.76]]
And it should work
Hope this answer helps.

Your Answer

Interviews

Parent Categories