我正在尝试使用sklearn库生成一个svm预测器。但是,每次我尝试运行fit(X,Y)
时,我都会得到以下错误:
类的数量必须大于1;有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...]
我想说你的形状有问题,试着在拟合你的模型之前这样做:
x_learn = x_learn.reshape(y_learn.shape)
# OR
y_learn = y_learn.reshape(x_learn.shape)
如果它钢blosed比尝试改变它的0
或升级你的sklearn版本,因为一些版本的sklearn有-1
作为标签的问题