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 I get 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)
The X and Y matrix of Independent Variable and Dependent Variable respectively to DataFrame from int64 Type so that it gets converted from 1D array to 2D array.. i.e X=pd.DataFrame(X) and Y=pd.dataFrame(Y) where pd is of pandas class in python. and thus feature scaling in-turn doesn't lead to any error!
You are just supposed to provide the
predict
method with the same 2D array, but with one value that you want to process (or more). In short, you can just replaceWith
And it should work.
EDIT: This answer became popular so I thought I'd add a little more explanation about ML. The short version: we can only use
predict
on data that is of the same dimensionality as the training data (X
) was.In the example in question, we give the computer a bunch of rows in
X
(with 2 values each) and we show it the correct responses iny
. When we want topredict
using new values, our program expects the same - a bunch of rows. Even if we want to do it to just one row (with two values), that row has to be part of another array.The problem is occurring when you run prediction on the array
[0.58,0.76]
. Fix the problem by reshaping it before you callpredict()
: