安装SGD分类器的问题



我正在阅读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),但这也不起作用。

问题是,如果进行,您传递的y_train_5使得每个值都相同

print(set(y_train_5))

您将只看到一个值。考虑进行分层列车测试拆分,以确保每一个级别最终都在列车和测试中结束。或者,你的y_ train不包含";5〃;并且y_train_5和y_test_5中的所有值都是"s";错误";。

您的目标向量将标签与整数5进行比较,但标签是字符串。因此,y_train_5的所有值评估为False,并且.fit()返回"0";一类";错误

将两个矢量定义更改为:

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

并且分类器CCD_ 5方法将在没有错误的情况下运行。

最新更新