gpt4 book ai didi

python - SGDClassifier 类的数量必须大于 1;上了1节课

转载 作者:行者123 更新时间:2023-12-01 08:18:33 26 4
gpt4 key购买 nike

因此,我已尽一切可能来解决此错误,这也是我巨大的挫败感。

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

最佳答案

我同意 Thremane D. Henry 的观点。
我昨天其实也遇到了类似的问题,你可以检查train_labels.shape,或者np.unique(train_labels)

如果你打印出一些训练数据,比如train[5:10],你就会发现问题。

它们是字符而不是int

将代码更改为 (label == '9')

关于python - SGDClassifier 类的数量必须大于 1;上了1节课,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54837056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com