GridSearchCV

noun

say it: “GRID SURCH SEE VEE” /ˈɡrɪd ˈsɝtʃ ˈsi ˈvi/

A scikit-learn tool that tries every combination of chosen settings with cross-validation and keeps the best-scoring model.

What is GridSearchCV?

GridSearchCV is scikit-learn's grid search: give it a model and a dictionary of settings to try, and fit() cross-validates every combination, then refits the best one on all the training rows. best_params_ and best_score_ say what won, and the fitted search predicts with it.

from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
fruit = [[120], [125], [130], [135], [180], [185], [190], [195]]   # grams
names = ["lemon"] * 4 + ["orange"] * 4
search = GridSearchCV(DecisionTreeClassifier(random_state=0), {"max_depth": [1, 2, 3]}, cv=2).fit(fruit, names)
print(search.best_params_)   # {'max_depth': 1}

In a sentence

  • “GridSearchCV picked a depth of 4 for the pipeline's tree.”
  • “After fitting, GridSearchCV predicts with the best model it found.”

Level: Freelancer · More Python terms at this level

Updated