Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try multiple estimator in one grid-search

Tags:

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 ?.

like image 831
tj89 Avatar asked Jul 24 '16 18:07

tj89


People also ask

Can pipeline have multiple estimators?

Pipeline can be used to chain multiple estimators into one. This is useful as there is often a fixed sequence of steps in processing the data, for example feature selection, normalization and classification.

What is the difference between GridSearchCV and RandomizedSearchCV?

The only difference between both the approaches is in grid search we define the combinations and do training of the model whereas in RandomizedSearchCV the model selects the combinations randomly. Both are very effective ways of tuning the parameters that increase the model generalizability.

What is exhaustive grid search?

One of the most important and generally-used methods for performing hyperparameter tuning is called the exhaustive grid search. This is a brute-force approach because it tries all of the combinations of hyperparameters from a grid of parameter values.


2 Answers

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) 
like image 160
j-a Avatar answered Oct 02 '22 19:10

j-a


    from sklearn.base import BaseEstimator     from sklearn.model_selection import GridSearchCV          class DummyEstimator(BaseEstimator):         def fit(self): pass         def score(self): pass              # Create a pipeline     pipe = Pipeline([('clf', DummyEstimator())]) # Placeholder Estimator          # Candidate learning algorithms and their hyperparameters     search_space = [{'clf': [LogisticRegression()], # Actual Estimator                      'clf__penalty': ['l1', 'l2'],                      'clf__C': np.logspace(0, 4, 10)},                                          {'clf': [DecisionTreeClassifier()],  # Actual Estimator                      'clf__criterion': ['gini', 'entropy']}]               # Create grid search      gs = GridSearchCV(pipe, search_space) 
like image 21
Brian Spiering Avatar answered Oct 02 '22 18:10

Brian Spiering