python 如何解决值错误:类的数量必须大于一;我得了一门课

jw5wzhpr  于 2022-12-25  发布在  Python
关注(0)|答案(2)|浏览(289)

当我运行以下代码时:

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)

我收到了ValueErrorThe number of classes has to be greater than one; got 1 class
我做了:print(np.unique(y_train)),它返回[0]
有人能给我指出解决问题的正确方向吗?

xienkqul

xienkqul1#

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

e0bqpujr

e0bqpujr2#

stratifytrain_test_split一起使用可降低这种可能性:

train_test_split(X, y, stratify=y)
    • 说明**:train_test_split具有随机性,有可能在y_train不包含正例和反例的情况下产生分裂,这意味着我们无法训练判别式分类器:
from sklearn.model_selection import train_test_split
import numpy as np

X = np.ones((8, 2))
y = np.array([0, 0, 0, 0, 0, 0, 1, 1])

_, _, y_train, y_test = train_test_split(X, y, random_state=33)
# y_train:   [0 0 0 0 0 0]    # <--- Uh oh, there are no 1s in the training set!
# y_test:    [1 1]
  • 分层 * 首先根据标签分隔数据。这意味着我们的训练数据应该 * 至少 * 具有以下每个标签中的一个:
_, _, y_train, y_test = train_test_split(X, y, stratify=y)
# y_train:   [0 0 0 0 1 0]
# y_test:    [0 1]

相关问题