Try multiple estimator in one grid-search

tj89 picture tj89 · Jul 24, 2016 · Viewed 11.3k times · Source

Is there a way we can grid-search multiple estimators at a time in Sklearn or any other library. For example can we pass SVM and Random Forest in one grid search ?.

Answer

j-a picture j-a · Oct 20, 2016

Yes. Example:

pipeline = Pipeline([
    ('vect', CountVectorizer()),
    ('clf', SGDClassifier()),
])
parameters = [
    {
        'vect__max_df': (0.5, 0.75, 1.0),
        'clf': (SGDClassifier(),),
        'clf__alpha': (0.00001, 0.000001),
        'clf__penalty': ('l2', 'elasticnet'),
        'clf__n_iter': (10, 50, 80),
    }, {
        'vect__max_df': (0.5, 0.75, 1.0),
        'clf': (LinearSVC(),),
        'clf__C': (0.01, 0.5, 1.0)
    }
]
grid_search = GridSearchCV(pipeline, parameters)