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)
I was facing the same issue earlier but I have somehow found the solution, You can try
reg.predict([[3300]])
.The API used to allow scalar value but now you need to give a 2D array.
With one feature my Dataframe list converts to a Series. I had to convert it back to a Dataframe list and it worked.
I faced the same problem. You just have to make it an array and moreover you have to put double squared brackets to make it a single element of the 2D array as first bracket initializes the array and the second makes it an element of that array.
So simply replace the last statement by:
I faced the same issue except that the data type of the instance I wanted to predict was a
panda.Series
object.Well I just needed to predict one input instance. I took it from a slice of my data.
In this case, you'll need to convert it into a 1-D array and then
reshape
it.From the docs,
values
will convert Series into a numpy array.Just insert the argument between a double square bracket:
regressor.predict([[values]])
that worked for me
I use the below approach.