Scikit学习交叉Val分数:数组索引过多 [英] Scikit-learn cross val score: too many indices for array
问题描述
我有以下代码
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.cross_validation import cross_val_score
#split the dataset for train and test
combnum['is_train'] = np.random.uniform(0, 1, len(combnum)) <= .75
train, test = combnum[combnum['is_train']==True], combnum[combnum['is_train']==False]
et = ExtraTreesClassifier(n_estimators=200, max_depth=None, min_samples_split=10, random_state=0)
min_samples_split=10, random_state=0 )
labels = train[list(label_columns)].values
tlabels = test[list(label_columns)].values
features = train[list(columns)].values
tfeatures = test[list(columns)].values
et_score = cross_val_score(et, features, labels, n_jobs=-1)
print("{0} -> ET: {1})".format(label_columns, et_score))
检查数组的形状:
features.shape
Out[19]:(43069, 34)
还有
labels.shape
Out[20]:(43069, 1)
我得到:
IndexError: too many indices for array
以及回溯的相关部分:
---> 22 et_score = cross_val_score(et, features, labels, n_jobs=-1)
我正在从Pandas数据帧中创建数据,我在这里搜索并看到一些通过此方法对可能的错误的引用,但无法弄清楚如何纠正? 数据数组是什么样的: 功能
I'm creating the data from Pandas dataframes and I searched here and saw some reference to possible errors via this method but can't figure out how to correct? What the data arrays look like: features
Out[21]:
array([[ 0., 1., 1., ..., 0., 0., 1.],
[ 0., 1., 1., ..., 0., 0., 1.],
[ 1., 1., 1., ..., 0., 0., 1.],
...,
[ 0., 0., 1., ..., 0., 0., 1.],
[ 0., 0., 1., ..., 0., 0., 1.],
[ 0., 0., 1., ..., 0., 0., 1.]])
标签
Out[22]:
array([[1],
[1],
[1],
...,
[1],
[1],
[1]])
推荐答案
当我们在scikit-learn中进行交叉验证时,该过程需要一个(R,)
形状标签而不是(R,1)
.尽管它们在某种程度上是相同的,但它们的索引编制机制却不同.因此,在您的情况下,只需添加:
When we do cross validation in scikit-learn, the process requires an (R,)
shape label instead of (R,1)
. Although they are the same thing to some extend, their indexing mechanisms are different. So in your case, just add:
c, r = labels.shape
labels = labels.reshape(c,)
将其传递给交叉验证功能之前.
before passing it to the cross-validation function.
这篇关于Scikit学习交叉Val分数:数组索引过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!