23
votes

I have the following code

 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))

Checking the shape of the arrays:

 features.shape
 Out[19]:(43069, 34)

And

labels.shape
Out[20]:(43069, 1)

and I'm getting:

IndexError: too many indices for array

and this relevant part of the traceback:

---> 22 et_score = cross_val_score(et, features, labels, n_jobs=-1)

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.]])

labels

Out[22]:
array([[1],
   [1],
   [1],
   ..., 
   [1],
   [1],
   [1]])
5
Please post the full traceback. Which version of scikit-learn are you on? And can you try to pass labels.ravel() instead? - Andreas Mueller
labels.ravel() did it! Was just reading another error , that suggest that using different code,, I'm on Scikit learn 17 dev0 - dartdog
@AndreasMueller Thank you so much for the quick response! Might help a few others if you can make an answer.. - dartdog
I don't think this is a good error. Can you please open an issue in the issue tracker? - Andreas Mueller
MMM where should I post the error to? Pandas or scikit-learn? - dartdog

5 Answers

36
votes

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.

15
votes

It seems to be fixable if you specify the target labels as a single data column from Pandas. If the target has multiple columns, I get a similar error. For example try:

labels = train['Y']
2
votes

Adding .ravel() to the Y/Labels variable passed into the formula helped solve this problem within KNN as well.

0
votes

try target:

y=df['Survived'] 

instead , i used

y=df[['Survived']] 

which made the target y a dateframe, it seems series would be ok

0
votes

You might need to play with the dimensions a bit, e.g.

et_score = cross_val_score(et, features, labels, n_jobs=-1)[:,n]

or

 et_score = cross_val_score(et, features, labels, n_jobs=-1)[n,:]

n being the dimension.