Search code examples
machine-learningclassificationgrid-searchmulticlass-classificationsvc

How to implement grid searchcv for onevsrestclassifier of SVC classifier


I want to do a grid search on OnevsRest Classifier and my model is SVC but it shows me the following error on using the grid search --how to resolve??

Code-

from sklearn.model_selection import GridSearchCV 

# defining parameter range 
param_grid = {'C': [0.1, 1, 10, 100, 1000],  
              'gamma': [1, 0.1, 0.01, 0.001, 0.0001], 
              'kernel': ['rbf']}  
svc_model_orc = OneVsRestClassifier(SVC())

grid = GridSearchCV(svc_model_orc, param_grid, refit = True, verbose = 3) 

# fitting the model for grid search 
grid.fit(X_train, y_train) 

# svc_pred_train=grid.predict(X_train)
# svc_pred_test = grid.predict(X_valid)

# print(accuracy_score(y_train, svc_pred_train))
# print(f1_score(y_train, svc_pred_train, average='weighted'))

# print(accuracy_score(y_valid, svc_pred_test))
# print(f1_score(y_valid, svc_pred_test, average='weighted'))

Error-

ValueError: Invalid parameter C for estimator OneVsRestClassifier(estimator=SVC(C=1.0, cache_size=200, class_weight=None,
                                  coef0=0.0, decision_function_shape='ovr',
                                  degree=3, gamma='auto_deprecated',
                                  kernel='rbf', max_iter=-1, probability=False,
                                  random_state=None, shrinking=True, tol=0.001,
                                  verbose=False),
                    n_jobs=None). Check the list of available parameters with `estimator.get_params().keys()`.

Solution

  • Since you're performing a GridSearch over nested estimators (even though you just have one, OneVsRestClassifier fits a classifier per class), you need to define the parameters with the syntax estimator__some_parameter.

    In the case of having nested objects, such as in pipelines for instance, this is the syntax GridSerach expects to access the different model's parameters, i.e. <component>__<parameter> . In such case, you'd name each model and then set their parameters as SVC__some_parameter for example for a SVC parameter. But for this case, the classifier is under estimator, note that the actual model is accessed through the estimator attribute:

    print(svc_model_orc.estimator)
    
    SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
        decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
        max_iter=-1, probability=False, random_state=None, shrinking=True,
        tol=0.001, verbose=False)
    

    So in this case, you should set the parameter grid as:

    param_grid = {'estimator__C': [0.1, 1, 10, 100, 1000],  
                  'estimator__gamma': [1, 0.1, 0.01, 0.001, 0.0001], 
                  'estimator__kernel': ['rbf']}