Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridSearch for Multilabel OneVsRestClassifier?

I'm doing a grid search over multilabel data as follows:

#imports
from sklearn.svm import SVC as classifier
from sklearn.pipeline import Pipeline
from sklearn.decomposition import RandomizedPCA
from sklearn.cross_validation import StratifiedKFold
from sklearn.grid_search import GridSearchCV

#classifier pipeline
clf_pipeline = clf_pipeline = OneVsRestClassifier(
                Pipeline([('reduce_dim', RandomizedPCA()),
                          ('clf', classifier())
                          ]
                         ))

C_range = 10.0 ** np.arange(-2, 9)
gamma_range = 10.0 ** np.arange(-5, 4)
n_components_range = (10, 100, 200)
degree_range = (1, 2, 3, 4)

param_grid = dict(estimator__clf__gamma=gamma_range,
                  estimator__clf__c=c_range,
                  estimator__clf__degree=degree_range,
                  estimator__reduce_dim__n_components=n_components_range)

grid = GridSearchCV(clf_pipeline, param_grid,
                                cv=StratifiedKFold(y=Y, n_folds=3), n_jobs=1,
                                verbose=2)
grid.fit(X, Y)

I'm seeing the following traceback:

/Users/andrewwinterman/Documents/sparks-honey/classifier/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit_grid_point(X, y, base_clf, clf_params, train, test, loss_func, score_func, verbose, **fit_params)
    107 
    108     if y is not None:
--> 109         y_test = y[safe_mask(y, test)]
    110         y_train = y[safe_mask(y, train)]
    111         clf.fit(X_train, y_train, **fit_params)

TypeError: only integer arrays with one element can be converted to an index

Looks like GridSearchCV objects to multiple labels. How should I work around this? Do I need to explicitly iterate through the unique classes with label_binarizer, running grid search on each sub-estimator?

like image 320
Maus Avatar asked Jan 08 '13 23:01

Maus


People also ask

What is OneVsRestClassifier?

OneVsRestClassifier(estimator, *, n_jobs=None, verbose=0)[source] One-vs-the-rest (OvR) multiclass strategy. Also known as one-vs-all, this strategy consists in fitting one classifier per class. For each classifier, the class is fitted against all the other classes.

What is GridSearchCV used for?

GridSearchCV is a technique for finding the optimal parameter values from a given set of parameters in a grid. It's essentially a cross-validation technique. The model as well as the parameters must be entered. After extracting the best parameter values, predictions are made.


1 Answers

I think there is a bug in grid_search.py

Have you tried to give y as numpy array?

import numpy as np
Y = np.asarray(Y)
like image 159
Baskaya Avatar answered Oct 19 '22 05:10

Baskaya