0
$\begingroup$

I'm trying to use GridSearchCV with RidgeClassifier, but I'm getting this error:

My problem is regression type.

IndexError: too many indices for array

I'm new to Machine Learning, please help me out. This is the code I've been trying to implement:

from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report

tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
scores = ['precision', 'recall']
alphas = np.array([1,0.1,0.01,0.001,0.0001,0])
model = RidgeClassifier(normalize=True, random_state=100, tol=0.1)
for score in scores:
    clf = GridSearchCV(estimator=model, param_grid=dict(alpha=alphas))
    clf.fit(X, Y)
    print("Best parameters set found on development set:")
    print(clf.best_params_)
    for params, mean_score, scores in clf.grid_scores_:
        print("%0.3f (+/-%0.03f) for %r"
              % (mean_score, scores.std() * 2, params))

This is the complete error log:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-59-c97c7e0fc6f3> in <module>()
     12 for score in scores:
     13     clf = GridSearchCV(estimator=model, param_grid=dict(alpha=alphas))
---> 14     clf.fit(X, Y)
     15     print("Best parameters set found on development set:")
     16     print(clf.best_params_)

/usr/local/lib/python2.7/dist-packages/sklearn/grid_search.pyc in fit(self, X, y)
    836 
    837         """
--> 838         return self._fit(X, y, ParameterGrid(self.param_grid))
    839 
    840 

/usr/local/lib/python2.7/dist-packages/sklearn/grid_search.pyc in _fit(self, X, y, parameter_iterable)
    551                                  'of samples (%i) than data (X: %i samples)'
    552                                  % (len(y), n_samples))
--> 553         cv = check_cv(cv, X, y, classifier=is_classifier(estimator))
    554 
    555         if self.verbose > 0:

/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.pyc in check_cv(cv, X, y, classifier)
   1833         if classifier:
   1834             if type_of_target(y) in ['binary', 'multiclass']:
-> 1835                 cv = StratifiedKFold(y, cv)
   1836             else:
   1837                 cv = KFold(_num_samples(y), cv)

/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.pyc in __init__(self, y, n_folds, shuffle, random_state)
    568         for test_fold_idx, per_label_splits in enumerate(zip(*per_label_cvs)):
    569             for label, (_, test_split) in zip(unique_labels, per_label_splits):
--> 570                 label_test_folds = test_folds[y == label]
    571                 # the test split can be too big because we used
    572                 # KFold(max(c, self.n_folds), self.n_folds) instead of

IndexError: too many indices for array
$\endgroup$
2
  • $\begingroup$ what do you get for X.shape and Y.shape? $\endgroup$
    – David Marx
    Feb 11, 2018 at 21:16
  • $\begingroup$ @DavidMarx (15780, 24) (15780, 1) , these are their respective shapes. $\endgroup$ Feb 12, 2018 at 9:09

1 Answer 1

1
$\begingroup$

The problem is that Y has shape (15780,1). The shape of Y should show (15780,). The explanation for this is a bit long, so I just give here the links-

Difference between shapes (R,1) and (R,)

Solution for IndexError: too many indices for array

$\endgroup$
3
  • $\begingroup$ How do I change this (15780, 1) to (15780, ) ? $\endgroup$ Feb 12, 2018 at 13:28
  • $\begingroup$ I replaced Y with Y.ravel() and got this error >AttributeError: 'numpy.float64' object has no attribute 'items' $\endgroup$ Feb 12, 2018 at 14:44
  • $\begingroup$ Try- Y = Y.reshape(15780,). It should solve the problem. $\endgroup$
    – Ankit Seth
    Feb 12, 2018 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.