Hyperparameter Tuning (GridSearchCV)
Stop guessing settings like k or max_depth — let the computer search for the best ones automatically.
What you will learn
- Tell parameters from hyperparameters
- Run GridSearchCV to find the best settings
- Know when to prefer RandomizedSearchCV
The settings you choose yourself
A model learns most things on its own — a line’s slope, a tree’s questions. But some settings you must choose before training: the k in KNN, the max_depth of a tree, the alpha of Ridge, the learning_rate of boosting. These chosen-in-advance settings are called hyperparameters.
| Parameter | Hyperparameter | |
|---|---|---|
| Who sets it | The model, during training | You, before training |
| Examples | Line slope, tree split points | k, maxdepth, alpha, learningrate |
| How to find good ones | Just call .fit() | Search and test (this lesson) |
So far you have tweaked these by hand, re-running and eyeballing the score. That is slow and easy to fool yourself with. Hyperparameter tuning automates the search.
Grid search: try every combination
GridSearchCV takes a grid of values you want to try and tests every combination, scoring each with cross-validation (so the result is trustworthy, not a lucky split). It then hands you the best combination. The “CV” in the name is exactly the cross-validation you already met.
Step by step, for a KNN where we want to try k = 1, 3, 5, 7:
- List the values to try:
{ 'n_neighbors': [1, 3, 5, 7] }. - GridSearchCV trains a KNN for each value of k.
- Each one is scored with cross-validation (e.g. 5 folds), giving a fair average.
- It compares the averages and keeps the best k.
- It reports the winning settings and best score.
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
grid = { 'n_neighbors': [1, 3, 5, 7, 9] }
search = GridSearchCV(KNeighborsClassifier(), grid, cv=5)
search.fit(X, y)
print('Best k: ', search.best_params_)
print('Best score:', round(search.best_score_, 3))Note: Output: Best k: {'n_neighbors': 5} Best score: 0.973 Instead of guessing, the search tried all five values of k with 5-fold cross-validation and found k = 5 gave the best average score (about 97.3%). It also stores the winning model ready to predict with.
Searching two hyperparameters at once
Grid search really earns its keep with several hyperparameters, where hand-tuning combinations becomes hopeless. For a tree you might tune depth and the split criterion together:
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
grid = {
'max_depth': [2, 3, 4, 5],
'criterion': ['gini', 'entropy'],
}
search = GridSearchCV(DecisionTreeClassifier(random_state=0), grid, cv=5)
search.fit(X, y)
print('Best settings:', search.best_params_)
print('Best score: ', round(search.best_score_, 3))Note: Output: Best settings: {'criterion': 'gini', 'max_depth': 3} Best score: 0.96 GridSearchCV tested all 4 × 2 = 8 combinations, each with 5-fold cross-validation (40 fits in total), and picked depth 3 with the gini criterion. Trying that by hand would be tedious and error-prone.
When the grid is too big: RandomizedSearchCV
Grid search tries every combination, so the count explodes fast: 5 settings × 4 settings × 3 settings = 60 combinations, each cross-validated. When the grid is huge, RandomizedSearchCV samples a fixed number of random combinations instead of all of them — usually finding a near-best answer in a fraction of the time.
| GridSearchCV | RandomizedSearchCV | |
|---|---|---|
| Tries | Every combination | A random sample of them |
| Best for | Small grids | Large grids / many hyperparameters |
| Guarantee | Finds the best in the grid | Usually finds a near-best, much faster |
Watch out: Tuning is slow: GridSearchCV trains combinations × folds models. Five settings with 5-fold CV is 25 fits; two hyperparameters quickly become hundreds. Start with a small, sensible grid and widen it only if needed.
Tip: Best practice: put your preprocessing and model in a pipeline (previous lesson), then hand the pipeline to GridSearchCV. The search then tunes hyperparameters and applies preprocessing correctly inside every fold — leak-free tuning.
Q. What is a hyperparameter?
✍️ Practice
- Extend the KNN grid to also try
'weights': ['uniform', 'distance']and read the best combination. - Explain in one sentence why RandomizedSearchCV is preferred when the grid has hundreds of combinations.
🏠 Homework
- Pick any model you have learned and list two hyperparameters worth tuning, the values you would try for each, and how many total fits a 5-fold GridSearchCV would run.