I am trying to use SKLearn to run an SVM model. I am just trying it out now with some sample data. Here is the data and the code:
import numpy as np
from sklearn import svm
import random as random
A = np.array([[random.randint(0, 20) for i in range(2)] for i in range(10)])
lab = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
clf = svm.SVC(kernel='linear', C=1.0)
clf.fit(A, lab)
FYI, when I run
import sklearn
sklearn.__version__
It outputs 0.17.
Now, when I run print(clf.predict([1, 1]))
, I get the following warning:
C:\Users\me\AppData\Local\Continuum\Anaconda2\lib\site-packages\sklearn\ut
ils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecat
ed in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.re
shape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contain
s a single sample.
DeprecationWarning)
It does give me a prediction, which is great. However, I find this weird for a few reasons.
I don't have a 1d array. If you print A, you get
array([[ 9, 12],
[ 2, 16],
[14, 14],
[ 4, 2],
[ 8, 4],
[12, 3],
[ 0, 0],
[ 3, 13],
[15, 17],
[15, 16]])
Which appears to me to be 2 dimensional. But okay, let's just say that what I have is in fact a 1D array. Let's try to change it using reshape
, as suggested by the error.
Same code as above, but now we have
A = np.array([[random.randint(0, 20) for i in range(2)] for i in range(10)]).reshape(-1,1)
But then this outputs an array of length 20, which makes no sense and is not what I want. I also tried it with reshape(1, -1)
but then this gives me a single observation / list with 20 items in it.
How can I reshape my data in numpy arrays so that I don't get this warning?
I looked at two answers on SO, and neither worked for me. Question 1 and Question 2. It seems that Q1 was actually 1D data and was solved using reshape
, which I tried and failed at. Q2 has an answer about how to track warnings and errors, which isn't what I want. The other answer is again an instance of a 1D array.