无法运行适合svm的方法(通过scikit-learn)

时间:2015-06-07 12:39:07

标签: python numpy scikit-learn svm

我正在尝试使用sklearn库生成svm预测器。 但是,每次我尝试运行fit(X,Y)时,我都会收到以下错误:

  

班级数必须大于一;得到1

我很确定问题出在y_learn var上,因为如果我将y_learn更改为第一个元素为1而其他元素为0的列表,它有效。

我的代码是:

clf = svm.SVC()

clf.fit(x_learn,y_learn)

其中

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]

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 个答案:

答案 0 :(得分:0)

我会说你的形状有问题,在适合你的模型之前尝试这样做:

x_learn = x_learn.reshape(y_learn.shape)

# OR

y_learn = y_learn.reshape(x_learn.shape)

如果钢铁闪烁而不是试图通过0进行更改或升级您的sklearn版本,因为某些版本的sklearn存在-1标签

的问题