如何解决 ValueError:类的数量必须大于 1;有 1 节课

问题描述

当我运行以下命令时:

from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)
clf=SVC(kernel='rbf',probability=True)
clf.fit(x_train,y_train)

我收到了 ValueError:The number of classes has to be greater than one; got 1 class 我使用了代码print(np.unique(y_train)),它返回了“[0]”。 任何人都可以为我指出解决方案的正确方向吗?

解决方法

要么您的 y 列表不包含 1,要么 y 中的 1 太少以至于 y_train 最终可能不包含 1。您应该打印 y,如果它包含 1,则需要更改拆分策略以确保所有类至少出现在 y_trainy_test 中一次

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...