scikit-learn 错误:y 中人口最少的类只有 1 个成员

Aur*_*ora 8 python scikit-learn train-test-split

我正在尝试使用train_test_splitscikit-learn 中的函数将我的数据集拆分为训练集和测试集,但出现此错误:

In [1]: y.iloc[:,0].value_counts()
Out[1]: 
M2    38
M1    35
M4    29
M5    15
M0    15
M3    15

In [2]: xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size=1/3, random_state=85, stratify=y)
Out[2]: 
Traceback (most recent call last):
  File "run_ok.py", line 48, in <module>
    xtrain,xtest,ytrain,ytest = train_test_split(X,y,test_size=1/3,random_state=85,stratify=y)
  File "/home/aurora/.pyenv/versions/3.6.0/lib/python3.6/site-packages/sklearn/model_selection/_split.py", line 1700, in train_test_split
    train, test = next(cv.split(X=arrays[0], y=stratify))
  File "/home/aurora/.pyenv/versions/3.6.0/lib/python3.6/site-packages/sklearn/model_selection/_split.py", line 953, in split
    for train, test in self._iter_indices(X, y, groups):
  File "/home/aurora/.pyenv/versions/3.6.0/lib/python3.6/site-packages/sklearn/model_selection/_split.py", line 1259, in _iter_indices
    raise ValueError("The least populated class in y has only 1"
ValueError: The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2.
Run Code Online (Sandbox Code Playgroud)

但是,所有类都至少有 15 个样本。为什么我收到这个错误?

X 是一个表示数据点的 Pandas DataFrame,y 是一个 Pandas DataFrame,其中一列包含目标变量。

我无法发布原始数据,因为它是专有的,但是通过创建一个具有 1k 行 x 500 列的随机 Pandas DataFrame (X) 和一个具有相同行数 (1k) X 的随机 Pandas DataFrame (y),它是相当可重现的,并且,对于每一行目标变量(分类标签)。y pandas DataFrame 应该有不同的分类标签(例如'class1'、'class2'...),并且每个标签应该至少出现 15 次。

use*_*939 11

要点是,如果您使用分层 CV,那么如果分割数无法产生数据中所有类的比例相同的所有 CV 分割,您将收到此警告。例如,如果一个类有 2 个样本,则将有 2 个包含该类 2 个样本的 CV 集,以及 3 个包含 0 个样本的 CV 集,因此该类的比率样本在所有 CV 集中并不相等。但问题仅在于任何集合中都有 0 个样本,因此如果您的样本数至少与 CV 分割数一样多,即本例中为 5 个,则不会出现此警告。

请参阅/sf/answers/3382017341/


小智 9

stratify=y拆分训练和测试数据时删除

xtrain, xtest, ytrain, ytest = train_test_split(X, y, test_size=1/3, random_state=85)
Run Code Online (Sandbox Code Playgroud)

  • 为什么要删除它?你能简单解释一下吗? (3认同)
  • 正如它所说,“stratify”是一个对训练/测试分割进行“分层”(意味着考虑传递特征的分布)的函数。如果删除它,您所做的事情与以前完全不同:您随机分割数据而不考虑任何分布。因此,这是一个非常具有误导性的答案,不应给予如此高的评价。 (2认同)

Aur*_*ora 5

问题是train_test_split将 2 个数组作为输入,但该y数组是一列矩阵。如果我只通过y它的第一列。

train, xtest, ytrain, ytest = train_test_split(X, y.iloc[:,1], test_size=1/3,
  random_state=85, stratify=y.iloc[:,1])
Run Code Online (Sandbox Code Playgroud)