. Advertisement .
..3..
. Advertisement .
..4..
I get an error:
Expected 2D array, got 1D array instead:
when I try to run the following code:
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]))
X.reshape(1, -1)
How to fix the valueerror: expected 2d array, got 1d array instead:. Please give me some good ideas.
The cause: This error occurs because the
predict
method is only supposed to be provided with the same 2D array but with the one value you wish to process.Solution:
You can simply replace.
With
This problem occurs when you run prediction against the array
[0.58,0.76]
. You can fix the problem by changing its shape before callingpredict()