当我运行以下代码时:
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]
。
有人能给我指出解决问题的正确方向吗?
2条答案
按热度按时间xienkqul1#
y
列表不包含1,或者y
中的1太少,以至于y_train
最终可能不包含1。您应该打印y
,如果它包含1,则需要更改拆分策略,以确保所有类在y_train
和y_test
中至少出现一次e0bqpujr2#
将
stratify
与train_test_split
一起使用可降低这种可能性:train_test_split
具有随机性,有可能在y_train
不包含正例和反例的情况下产生分裂,这意味着我们无法训练判别式分类器: