我正在尝试使用Scikit SVM进行二进制分类。
我使用以下函数创建了数据集:
from sklearn.datasets.base import Bunch
def read(dataset,target_index=0):
if target_index == -1:
data = dataset[:, :-1]
target = dataset[:, -1]
elif target_index == 0:
data = dataset[:, 1:]
target = dataset[:, 0]
return Bunch(data=data, target=target)
digits=read(dataset,target_index=0)
print target.shape
#(1968,)
print data.shape
#(1968, 784)
classifier = svm.SVC(gamma=0.001)
在接下来的一行中,我收到一个错误: ValueError:类的数量必须大于一;得到1 :
classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2])
还试过以下哪些不起作用:
ValueError: The number of classes has to be greater than one; got 1
Python: ValueError: The number of classes has to be greater than one; got 1
样本数据
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 41 146 146 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 129 253 253 253 250 163 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 133 253 253 253 253 253 253 229 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 101 253 252 145 102 107 237 253 247 128 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 181 253 167 0 0 0 61 235 253 253 163 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 253 43 0 0 0 0 58 193 253 253 164 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 187 253 32 0 0 0 0 0 55 236 253 253 86 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 253 32 0 100 190 87 87 87 147 253 253 123 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 94 253 78 40 248 253 253 253 253 253 253 253 223 84 15 0 0 0 0 0 0 0 0 0 0 0 0 0 14 92 12 35 240 253 253 253 253 253 253 253 253 253 244 89 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 75 161 179 253 253 253 253 253 253 253 253 253 209 43 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 16 16 39 38 16 16 145 243 253 253 185 48 0 0 0 0 0 0 0 0 0 0 0 0 0 20 58 0 0 0 0 0 0 0 0 58 209 253 253 183 0 0 0 0 0 0 0 0 0 0 0 0 77 221 247 79 0 0 0 0 0 0 0 0 13 219 253 240 72 0 0 0 0 0 0 0 0 0 0 0 90 247 253 252 57 0 0 0 0 0 0 0 0 53 251 253 191 0 0 0 0 0 0 0 0 0 0 0 0 116 253 253 59 0 0 0 0 0 0 0 0 99 252 253 145 0 0 0 0 0 0 0 0 0 0 0 0 14 188 253 221 158 38 0 0 0 0 111 211 246 253 253 145 0 0 0 0 0 0 0 0 0 0 0 0 0 12 221 246 253 251 249 249 249 249 253 253 253 253 200 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 95 183 228 253 253 253 253 253 253 195 124 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 37 138 74 126 88 37 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 106 255 225 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 156 253 253 253 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 209 253 253 253 198 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 204 254 253 253 198 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 236 253 254 253 198 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98 253 253 251 169 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 29 207 253 253 242 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 189 253 253 207 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 119 249 253 253 132 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 222 253 253 201 11 0 0 0 82 122 87 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 186 254 254 250 23 12 134 203 254 255 254 254 214 19 0 0 0 0 0 0 0 0 0 0 0 0 0 61 253 253 245 105 134 249 254 253 253 253 253 253 253 36 0 0 0 0 0 0 0 0 0 0 0 0 6 182 253 253 221 240 253 253 254 250 217 224 253 253 200 14 0 0 0 0 0 0 0 0 0 0 0 0 60 253 253 253 253 253 253 230 200 76 7 145 253 253 145 0 0 0 0 0 0 0 0 0 0 0 0 0 157 253 253 253 253 253 253 161 30 6 146 253 253 115 14 0 0 0 0 0 0 0 0 0 0 0 0 0 157 253 253 253 253 253 253 253 223 199 253 253 201 43 0 0 0 0 0 0 0 0 0 0 0 0 0 0 157 253 253 253 253 253 253 253 254 253 253 237 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 157 253 253 253 253 253 253 253 254 253 193 127 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 106 253 253 253 253 253 253 253 249 132 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 93 132 225 253 253 201 132 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
数据注释:数据的第一列是目标值,而其他784列在0-255之间,表示手写数字的28 * 28图像大小。