类型错误:单例数组 array(True) 不能被视为有效集合

spe*_*ark 7 python numpy dataframe pandas scikit-learn

我想将我拥有的数据集拆分为测试/训练,同时确保分类标签在测试/训练中的分布相同。为此,我使用了分层选项,但它会引发如下错误:

X_full_train, X_full_test, Y_full_train, Y_full_test = train_test_split(X_values_full, Y_values, test_size = 0.33, random_state = 42, stratify = True)
Run Code Online (Sandbox Code Playgroud)

错误信息:

TypeError                                 Traceback (most recent call last)
 in 
     19 
     20 
---> 21 X_full_train, X_full_test, Y_full_train, Y_full_test = train_test_split(X_values_full, Y_values, test_size = 0.33, random_state = 42, stratify = True)
     22 
     23 

~/anaconda3/lib/python3.8/site-packages/sklearn/model_selection/_split.py in train_test_split(*arrays, **options)
   2150                      random_state=random_state)
   2151 
-> 2152         train, test = next(cv.split(X=arrays[0], y=stratify))
   2153 
   2154     return list(chain.from_iterable((_safe_indexing(a, train),

~/anaconda3/lib/python3.8/site-packages/sklearn/model_selection/_split.py in split(self, X, y, groups)
   1744         to an integer.
   1745         """
-> 1746         y = check_array(y, ensure_2d=False, dtype=None)
   1747         return super().split(X, y, groups)
   1748 

~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
     71                           FutureWarning)
     72         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73         return f(**kwargs)
     74     return inner_f
     75 

~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    647 
    648     if ensure_min_samples > 0:
--> 649         n_samples = _num_samples(array)
    650         if n_samples < ensure_min_samples:
    651             raise ValueError("Found array with %d sample(s) (shape=%s) while a"

~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in _num_samples(x)
    194     if hasattr(x, 'shape') and x.shape is not None:
    195         if len(x.shape) == 0:
--> 196             raise TypeError("Singleton array %r cannot be considered"
    197                             " a valid collection." % x)
    198         # Check that shape is returning an integer or default to len

TypeError: Singleton array array(True) cannot be considered a valid collection.
Run Code Online (Sandbox Code Playgroud)

当我尝试在没有分层选项的情况下执行此操作时,它不会给我错误。我认为这是因为我的 Y 标签没有在测试/训练之间均匀分布标签所需的最少样本数,但是:

pp.pprint(Counter(Y_values))
Run Code Online (Sandbox Code Playgroud)

给出:

Counter({13: 1084,
         1: 459,
         7: 364,
         8: 310,
         38: 295,
         15: 202,
         4: 170,
         37: 105,
         3: 98,
         0: 85,
         24: 79,
         20: 78,
         35: 76,
         2: 75,
         12: 74,
         39: 72,
         22: 71,
         9: 63,
         26: 59,
         11: 55,
         18: 55,
         32: 53,
         19: 53,
         33: 53,
         5: 52,
         30: 42,
         29: 42,
         25: 41,
         10: 39,
         23: 38,
         21: 38,
         6: 38,
         27: 37,
         14: 36,
         36: 36,
         34: 34,
         28: 33,
         17: 31,
         31: 30,
         16: 30})
Run Code Online (Sandbox Code Playgroud)

Jer*_* M. 14

根据sklearn documentation

类似stratifyarray,默认=无如果不是无,数据以分层方式分割,使用它作为类标签。

因此,它不接受booleanTrueorFalse之类的值,而是类标签本身。

所以,你需要改变:

X_full_train, X_full_test, Y_full_train, Y_full_test = train_test_split(X_values_full, Y_values, test_size = 0.33, random_state = 42, stratify = True)
Run Code Online (Sandbox Code Playgroud)

到:

X_full_train, X_full_test, Y_full_train, Y_full_test = train_test_split(X_values_full, Y_values, test_size = 0.33, random_state = 42, stratify = Y_values)
Run Code Online (Sandbox Code Playgroud)