【问题标题】:Issue fitting a SGD Classifier问题拟合 SGD 分类器
【发布时间】:2022-01-10 16:26:32
【问题描述】:

我正在关注 Aurelien Geron 的“动手机器学习”一书,更具体地说,它开始进入分类器。我正在遵循书中的代码,但我得到的错误是:

ValueError: The number of classes has to be greater than one; got 1 class

我的代码:

from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = .20, random_state = 42)

y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)

sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train_5)

当我在网上查找错误时,一个潜在的解决方法是使用np.unique(y_train_5),但这也不起作用。

【问题讨论】:

  • 什么是Xy
  • 如果您正在训练分类器,您的响应变量必须包含 1 个以上的唯一值。现在只有 5 个
  • 有趣的是,这本书会以这种方式呈现。我只做了 y_train,它按预期工作。

标签: python machine-learning scikit-learn


【解决方案1】:

问题是你传递了 y_train_5 使得每个值都是相同的,如果你这样做了

print(set(y_train_5))

您只会看到一个值。考虑进行分层训练测试拆分,以确保每个类最终都在训练和测试中。或者,您的 y_train 根本不包含“5”,并且 y_train_5 和 y_test_5 中的所有值都是“False”。

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 2018-06-02
    • 2021-07-31
    • 2019-07-09
    • 1970-01-01
    • 2021-10-27
    • 2021-04-04
    • 2014-05-13
    • 2019-08-31
    相关资源
    最近更新 更多