3

I'm trying to use OneVsRestClassifier with SVC for a multi classification problem for images - I got numerical features of the images from CellProfiler. I want to use GridSearchCV to find the hyperparameters to use and I am stuck.

Does anybody have a solution/suggestion to this?

I've read over the Google but it seems like I can't resolve my issue.

    grid = GridSearchCV(pipe, scoring='f1',
                       param_grid=param_grid, cv=5,
                       return_train_score=True,
                       iid=False,
                       n_jobs=-1
                       )
    grid.fit(X_train, np.ravel(y_train))
    return grid
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import classification_report

pipe = make_pipeline(StandardScaler(),
                     OneVsRestClassifier(SVC(probability=True)))

param_grid = {
    'estimator__C': [0.001, 0.01, 0.1, 1, 10, 100],
    'estimator__kernel': ['linear', 'rbf', 'poly'],
    'estimator__degree': [2, 3, 4, 5, 7, 10],
    'estimator__gamma': [0.01, 0.02, 0.03, 0.04, 0.05, 1]
}

clf = grid_search_fit(pipe, param_grid)

preds = clf.predict(X_test)
print(classification_report(y_test, preds, target_names = ['empty', 'good', 'blurred']))
ValueError: Invalid parameter estimator for estimator Pipeline(memory=None,
         steps=[('standardscaler',
                 StandardScaler(copy=True, with_mean=True, with_std=True)),
                ('onevsrestclassifier',
                 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=True,
                                                   random_state=None,
                                                   shrinking=True, tol=0.001,
                                                   verbose=False),
                                     n_jobs=None))],
         verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.

1 Answers1

0

I made some adaptations to your code as follows:

  1. Removed option iid=False
  2. I changed the shape of your Pipeline and GridSearchCV a little bit

The changed code follows. You can build Pipeline and Gridsearch more or less like this.

from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import classification_report

pipe = Pipeline([
    ("scale", StandardScaler()),
    ('classify', OneVsRestClassifier(SVC(probability=True)))
])
    
param_grid = {
    'classify__estimator__C': [0.001, 0.01, 0.1, 1, 10, 100],
    'classify__estimator__kernel': ['linear', 'rbf', 'poly'],
    'classify__estimator__degree': [2, 3, 4, 5, 7, 10],
    'classify__estimator__gamma': [0.01, 0.02, 0.03, 0.04, 0.05, 1]
}

grid_search = GridSearchCV(
    pipe, param_grid, cv=5, scoring='f1', verbose=1, return_train_score=True, n_jobs=-1)

grid_search = grid_search.fit(X_train, np.ravel(y_train))

preds = clf.predict(X_test)
print(classification_report(y_test, preds, target_names = ['empty', 'good', 'blurred']))