Unable to run the method fit for svm (by scikit-learn)

3.2k views Asked by At

I am trying to generate an svm predictor using the sklearn library. But, every time I try to run fit(X,Y) I get the following error:

The number of classes has to be greater than one; got 1

I am pretty sure the problem is with the y_learn var because if I change y_learn to a list where the first element is 1 and the others are 0, it works.

My code is:

clf = svm.SVC()

clf.fit(x_learn,y_learn)

where

y_learn = [ 1 -1 -1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1  1  1  1  1  1  1  1 -1 -1 -1 -1
  1  1  1  1  1  1  1  1  1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1  1  1  1  1  1
  1  1  1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1  1 -1 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1  1 -1 -1 -1 -1 -1 -1 -1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1]

and type(y_learn) = type 'numpy.ndarray'

x_learn = [array([  9.40768535e-01,   8.41994398e-01,   9.32081721e-01,
         5.66162508e-02,   7.98723422e-03,   1.43783134e-02,
        -7.09941391e-03,  -3.47126563e-03,   7.56540837e+01]), array([  9.51510849e-01,   8.45112974e-01,   9.38219301e-01,
         5.82776713e-02,   8.91018076e-03,   1.14186585e-02,
         1.43783134e-02,  -7.09941391e-03,   7.77932310e+01]), array([  9.55239672e-01,   8.48133424e-01,   9.41803516e-01,
         6.00029472e-02,   1.16427455e-02,   3.91884410e-03,
         1.14186585e-02,   1.43783134e-02,   7.84959346e+01]), array([  9.52616068e-01,   8.51255512e-01,   9.45513746e-01,
         6.13091486e-02,   1.15153207e-02,  -2.74653979e-03,
         3.91884410e-03,   1.14186585e-02,   7.66670540e+01]), array([  9.67841234e-01,   8.54751516e-01,   9.53595272e-01,
         6.28853797e-02,   9.68865724e-03,   1.59824778e-02,
        -2.74653979e-03,   3.91884410e-03,   7.96194885e+01]), array([  9.73522265e-01,   8.58377874e-01,   9.60146018e-01,
         6.44142845e-02,   9.91815056...]
1

There are 1 answers

7
farhawa On

I would say that you have a problem with your shapes, try to do this before fitting your model:

x_learn = x_learn.reshape(y_learn.shape)

# OR

y_learn = y_learn.reshape(x_learn.shape)

If it steel blosued than try to change it by 0 or upgrade your sklearn version because some version of sklearn have problem with -1 as label