我想将我拥有的数据集拆分为测试/训练,同时确保分类标签在测试/训练中的分布相同。为此,我使用了分层选项,但它会引发如下错误:
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)
错误信息: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.
当我尝试在没有分层选项的情况下执行此操作时,它不会给我错误。 我认为这是因为我的 Y 标签没有在测试/训练之间均匀分布标签所需的最少样本数,但是:pp.pprint(Counter(Y_values))
给出: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})
最佳答案
stratifyarray-like, default=None If not None, data is split in a stratified fashion, using this as the class labels.
因此,它不接受
boolean
值如 True
或 False
,但类标签自己。所以,你需要改变:
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)
到: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)
关于python - 类型错误 : Singleton array array(True) cannot be considered a valid collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63851453/