使用 sklearn 的 StratifiedKFold 函数,有人可以帮助我理解这里的错误吗?
我的猜测是它与我的标签输入数组有关,我注意到当我打印它们(本例中的前 16 个)时,索引从 0 变为 15,但上面打印了一个额外的 0 我没想到。也许我只是一个蟒蛇菜鸟,但这看起来很奇怪。
有人看到这里的傻瓜了吗?
文档:http://scikit-learn.org...分层KFold.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 FilesAnaconda2libsite-packagessklearncross_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
检查skew_gendata_targets.values
的形状。你会看到它不是StratifiedKFold所期望的一维数组(形状(500,)),而是一个(500,1)数组。SKlearn将它们分开对待,而不是强迫它们相同。让我知道这是否有帮助