Calmcode - scikit learn: search

Implement GridSearch in Scikit-Learn

1 2 3 4 5 6 7 8 9 10

We're only running a very basic variant of gridsearch with the code in this lesson, but it should serve as a general starting point for other pipelines.

from sklearn.datasets import load_boston
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
import pandas as pd

mod = GridSearchCV(estimator=pipe,
                   param_grid={
                     'model__n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
                   },
                   cv=3)
mod.fit(X, y);

Note that we can inspect the results of the grid via this command;

pd.DataFrame(mod.cv_results_)

If you're interested in more depth, feel free to check our this series of videos on metrics. Only start watching that after watching this full series though. The most important point of this series is made in the next videos.