StratifiedShuffleSplit: ValueError: y 中最少填充的类只有 1 个成员,太少了。

use*_*770 0 machine-learning python-3.x scikit-learn

我正在使用 StratifiedShuffleSplit 交叉验证器来预测波士顿数据集中的房价。当我运行下面的示例代码时。

def fit_model_S(labels, features,step, clf,parameters):
  cv = StratifiedShuffleSplit(n_splits=2,test_size=0.10, random_state = 42)
  print (cv)
  for train_index, test_index in cv.split(features,labels):
    labels_train, labels_test = labels[train_index], labels[test_index]
    features_train, features_test = features[train_index], features[test_index]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误。该代码适用于 ShuffleSplit。这是否意味着 StratifiedShuffleSplit 不能与数字标签一起使用。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-141-b290147edcbf> in <module>()
     33 dt_steps = [('decision', clf)]
     34 
---> 35 fit_model_S(labels, features,dt_steps,clf,parameters4)  
     36 
     37 

<ipython-input-141-b290147edcbf> in fit_model_S(labels, features, step, clf, parameters)
      8     cv = StratifiedShuffleSplit(n_splits=2,test_size=0.10, random_state = 42)
      9     print (cv)
---> 10     for train_index, test_index in cv.split(features,labels):
     11 
     12         labels_train, labels_test = labels[train_index], labels[test_index]

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py in split(self, X, y, groups)
   1194         """
   1195         X, y, groups = indexable(X, y, groups)
-> 1196         for train, test in self._iter_indices(X, y, groups):
   1197             yield train, test
   1198 

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py in _iter_indices(self, X, y, groups)
   1535         class_counts = np.bincount(y_indices)
   1536         if np.min(class_counts) < 2:
-> 1537             raise ValueError("The least populated class in y has only 1"
   1538                              " member, which is too few. The minimum"
   1539                              " number of groups for any class cannot"

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)

数据集示例如下。

      RM  LSTAT  PTRATIO      MEDV
0  6.575   4.98     15.3  504000.0
1  6.421   9.14     17.8  453600.0
2  7.185   4.03     17.8  728700.0
3  6.998   2.94     18.7  701400.0
4  7.147   5.33     18.7  760200.0
Run Code Online (Sandbox Code Playgroud)

在这种情况下,MEDV 是标签。

Viv*_*mar 5

波士顿住房数据是回归问题的数据集。您正在使用StratifiedShuffleSplit将其分为训练和测试。StratifiedShuffleSplit文档中所述:

这个交叉验证对象是 StratifiedKFold 和 ShuffleSplit 的合并,它返回分层的随机折叠。通过保留每个类别的样本百分比来进行折叠。

请看最后一行:-“保留每个班级的样本百分比”。因此,StratifiedShuffleSplit尝试将y值视为单个类。

但这是不可能的,因为您y是回归变量(连续数值数据)。

请查看 ShuffleSplit 或 train_test_split 来划分您的数据。有关交叉验证的更多详细信息,请参见此处:http : //scikit-learn.org/stable/modules/cross_validation.html#cross-validation