因此,我非常沮丧地竭尽所能解决此错误。
from sklearn.linear_model import SGDClassifier
train_labels_9 = [(label == 9) for label in train_labels_9]
test_labels_9 = [(label == 9) for label in test_labels_9]
sgd = SGDClassifier(max_iter = 1000, tol = 1e-3)
sgd.fit(train_images,train_labels_9)
below is the error
ValueError Traceback (most recent call last)
<ipython-input-57-8ad0fdf39a29> in <module>
6
7 sgd = SGDClassifier(max_iter = 1000, tol = 1e-3)
----> 8 sgd.fit(train_images,train_labels_9)
~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in fit(self, X, y, coef_init, intercept_init, sample_weight)
741 loss=self.loss, learning_rate=self.learning_rate,
742 coef_init=coef_init, intercept_init=intercept_init,
--> 743 sample_weight=sample_weight)
744
745
~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in _fit(self, X, y, alpha, C, loss, learning_rate, coef_init, intercept_init, sample_weight)
594
595 self._partial_fit(X, y, alpha, C, loss, learning_rate, self._max_iter,
--> 596 classes, sample_weight, coef_init, intercept_init)
597
598 if (self._tol is not None and self._tol > -np.inf
~\Anaconda3\lib\site-packages\sklearn\linear_model\stochastic_gradient.py in _partial_fit(self, X, y, alpha, C, loss, learning_rate, max_iter, classes, sample_weight, coef_init, intercept_init)
557 raise ValueError(
558 "The number of classes has to be greater than one;"
--> 559 " got %d class" % n_classes)
560
561 return self
ValueError: The number of classes has to be greater than one; got 1 class
答案 0 :(得分:0)
我同意Thremane D. Henry。
昨天我实际上遇到了类似的问题,您可以检查train_labels.shape
或np.unique(train_labels)
。
如果您打印出一些火车数据,请说train[5:10]
,您会发现问题。
它们是字符而不是int
。
将代码更改为(label == '9')
。
答案 1 :(得分:0)
您的标签似乎是文本值,因此当您执行以下语句时,它将返回仅包含False
的数组。
train_labels_9 = [(label == 9) for label in train_labels_9]
您可以使用
将标签转换为整数label = label.astype(np.uint8)