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)
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With