问题描述
当我运行以下命令时:
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_train
和 y_test
中一次