使用sklearn的StratifiedKFold功能,有人可以帮我理解这里的错误吗?
我的猜测是它与我的输入标签数组有关,我注意到当我打印它们时(本例中的前16个)索引从0到15,但是在我之上打印了一个额外的0没有期待。也许我只是一个蟒蛇菜鸟,但这看起来很奇怪。
有人在这里看到了蠢事吗?
文档:http://scikit-learn.org...StratifiedKFold.html
代码:
import nltk
import sklearn
print('The nltk version is {}.'.format(nltk.__version__))
print('The scikit-learn version is {}.'.format(sklearn.__version__))
print type(skew_gendata_targets.values), skew_gendata_targets.values.shape
print skew_gendata_targets.head(16)
skew_sfold10 = cross_validation.StratifiedKFold(skew_gendata_targets.values, n_folds=10, shuffle=True, random_state=20160121)
结果
The nltk version is 3.1.
The scikit-learn version is 0.17.
<type 'numpy.ndarray'> (500L, 1L)
0
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 1
15 0
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-373-653b6010b806> in <module>()
8 print skew_gendata_targets.head(16)
9
---> 10 skew_sfold10 = cross_validation.StratifiedKFold(skew_gendata_targets.values, n_folds=10, shuffle=True, random_state=20160121)
11
12 #print '\nSkewed Generated Dataset (', len(skew_gendata_data), ')'
d:\Program Files\Anaconda2\lib\site-packages\sklearn\cross_validation.pyc in __init__(self, y, n_folds, shuffle, random_state)
531 for test_fold_idx, per_label_splits in enumerate(zip(*per_label_cvs)):
532 for label, (_, test_split) in zip(unique_labels, per_label_splits):
--> 533 label_test_folds = test_folds[y == label]
534 # the test split can be too big because we used
535 # KFold(max(c, self.n_folds), self.n_folds) instead of
IndexError: too many indices for array
答案 0 :(得分:13)
检查skew_gendata_targets.values
的形状。你会发现它不是像StratifiedKFold所期望的1d数组(形状(500,)),而是(500,1)数组。 SKlearn分开处理这些,而不是强迫它们相同。如果有帮助,请告诉我