我想将数据集拆分为test/train,同时确保test/train中分类标签的分布相同。为此,我使用了stratify选项,但它抛出了如下错误:
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})
2条答案
按热度按时间nukf8bse1#
根据
sklearn documentation
:类分层数组,默认值= None如果不是None,则以分层方式拆分数据,并将其用作类标签。
因此,它不接受
boolean
值(如True
或False
),而是接受类标签本身。因此,您需要更改:
致:
tzxcd3kk2#
我认为这个错误是因为你的标签是连续的,而不是分类的。本质上,strative不能处理有很多唯一值的连续列。一个简单的解决方案是创建一个新列,将你的
Y_label
编码为分类列,例如Y_label_cat
:Y_values_cat = pd.qcut(Y_values,[0, .25, .5, .75, 1.],labels = ["1st","2nd","3rd","4th"])
,然后可以像这样传递参数:stratify = Y_label_cat
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_label_cat)