sklearn pipeline and grid search

Question:

from sklearn.linear_model import LogisticRegression
pipe4 = Pipeline([('ss', StandardScaler()), ('clf', knn)])

grid2 = GridSearchCV(pipe4, {'clf':[ knn, LogisticRegression()]})

grid2.fit(X_train, y_train)
pd.DataFrame(grid2.cv_results_).T

I made a knn classifier and logistic regression model and wanted to check which model is better through pipeline method.

as you can see the code above I put the knn only in the pipe4 but in grid search, both knn and logsistic regression are working and I could check the result

does it mean I can add the models in Gridseacrh even though I put the one model in pipeline?

Asked By: Nini

||

Answers:

Sure. As long as the estimator given to the GridSearchCV (in your example: pipe4) supports the parameters passed to param_grid (in your example: 'clf'), you can pass any values to the estimator’s parameters in the grid search (in your example: [knn, LogisticRegression()]).

Answered By: msamsami