【发布时间】:2021-10-10 03:12:35
【问题描述】:
所以我已经尽一切可能解决这个错误,我也非常沮丧。
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
【问题讨论】:
-
[(label == 9) for label in train_labels_9]那些train_labels_9包含的不是9?请同时发布一些标签示例。 -
@chris , labels = mnist['target'] labels_9 = (labels == '9'), from sklearn.model_selection import train_test_split train_images, test_images, train_labels_9, test_labels_9 = \ train_test_split(images, labels_9 , test_size = 0.15)
-
这不是一个例子。那只是另一个我们不知道里面是什么的代码:(
-
我明白了,所以你想让我解释一下代码的作用吗?只是需要一些清晰度PLZ克里斯
-
没有。我(通常也是如此)想要的是让您发布
train_label_9是什么。你可以print(train_label_9)复制粘贴,或者如果太长,切片会更好。为了将来参考,阅读How to create Minimal, Complete and Verifiable example 将帮助您更轻松、更快地获得答案。
标签: python