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
X.shape
andY.shape
? $\endgroup$