Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do GridSearchCV with OneVsRestClassifier?

I have a multiple-label problem. I use OneVsRestClassifier with SVM. Now I want tuning the parameter by GridSearchCV. I tried

GridSearchCV(estimator=OneVsRestClassifier(svm.SVC(probability=True)), param_grid=dict(C=Cs),
                      n_jobs=-1)

It returns various error info.

How to do GridSearchCV with OneVsRestClassifier? Maybe I should do a pipeline? However, it seems the relationship between OneVsRestClassifier with SVM is not as the pipeline function expected.


I also tried the code below. However, I cannot pass the parameter into the svm.SVC.

parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svr = OneVsRestClassifier(svm.SVC(probability=True))
clf = grid_search.GridSearchCV(svr, parameters)
clf.fit(X_ptrain, y_ptrain)
like image 337
Jill Clover Avatar asked Apr 02 '16 20:04

Jill Clover


People also ask

What is GridSearchCV Best_score_?

best_score_ is the average of all cv folds for a single combination of the parameters you specify in the tuned_params . In order to access other relevant details about the grid searching process, you can look at the grid.

What is Param grid in GridSearchCV?

estimator: estimator object being used. param_grid: dictionary that contains all of the parameters to try. scoring: evaluation metric to use when ranking results. cv: cross-validation, the number of cv folds for each combination of parameters.

How much time does GridSearchCV take?

Only ~7.5k records were used for training with cv=3, and ~3k records for testing purpose. Observing the above time numbers, for parameter grid having 3125 combinations, the Grid Search CV took 10856 seconds (~3 hrs) whereas Halving Grid Search CV took 465 seconds (~8 mins), which is approximate 23x times faster.

Is GridSearchCV stratified?

Judging by the documentation if you specify an integer GridSearchCV already uses stratified KFold in some cases: "For integer/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used.


1 Answers

You can use estimator to refer to parameters of SVC as shown below:

parameters = {'estimator__kernel':('linear', 'rbf'), 'estimator__C':[1, 10]}
svr = OneVsRestClassifier(svm.SVC(probability=True))
clf = grid_search.GridSearchCV(svr, parameters)
clf.fit(X_ptrain, y_ptrain)
like image 159
Abdul Avatar answered Oct 18 '22 22:10

Abdul