【问题标题】:SGDClassifier The number of classes has to be greater than one; got 1 classSGDClassifier 类的数量必须大于一;上了一堂课
【发布时间】: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


【解决方案1】:

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

如果你打印出一些你的火车数据,比如train[5:10],你就会发现问题所在。

它们是字符而不是 int

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

【讨论】:

    【解决方案2】:

    您的标签似乎是文本值,因此当您执行以下语句时,它返回一个仅包含 False 的数组。

    train_labels_9 = [(label == 9) for label in train_labels_9]
    

    您可以使用将标签转换为整数

    label = label.astype(np.uint8)
    

    【讨论】:

      【解决方案3】:

      (label == 9) 生成所有 False 向量 它将标签视为“9”(字符)

      【讨论】:

      • @neuron 如果您只看到这篇文章中的代码,那么您错过了一些东西。
      猜你喜欢
      • 2020-08-08
      • 2021-07-22
      • 1970-01-01
      • 2017-04-08
      • 2021-12-10
      • 2017-10-29
      • 2017-08-17
      • 2010-12-31
      • 2016-08-02
      相关资源
      最近更新 更多