Scikit-learn cross val score:数组的索引太多 [英] Scikit-learn cross val score: too many indices for array

查看:20
本文介绍了Scikit-learn cross val score:数组的索引太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

 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-learn cross val score:数组的索引太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆