Hyperparameter Optimization
Intermediate 14 min read Hyperparameter Optimization

Grid Search

The brute-force standard everyone benchmarks against

Grid search evaluates every combination of a discrete hyperparameter grid with cross validation. It's exhaustive, honest and embarrassingly parallel — but vulnerable to the dimensionality curse as the grid grows.

grid searchGridSearchCVcombinatorialexhaustive searchtuning
Before we start

What you will learn

  • Run GridSearchCV over a sensible grid
  • Explain combinatorial explosion and laggard-cost pitfalls
  • Design grids that stay evaluable
Introduction

What is this?

Grid search defines a set of candidate values per hyperparameter and evaluates the model over the Cartesian product — every combination — scoring each with cross validation. The best-scoring config wins. It is the simplest, most reproducible tuning method and the baseline every smarter strategy must beat, though its cost grows exponentially with the number of settings you sweep.

Motivation

Why do you need it?

  • It's exhaustive within a defined space — you know your evaluation touched every corner.
  • It is trivial to parallelize: each config is an independent job.
  • Results are auditable ('we tried these 120 combos, this one won').
  • It gives a complete response surface if you bother to inspect intermediate results.
Think about it

Mental model

Imagine

Tasting every combination on a menu

Think of

Grid search

Working through the whole combinatorial menu guarantees you find the best dish among what's offered — but the menu doubles every time you add a 'choice of side', and most of the plate is barely edible. Grid search is exhaustive, but it's only as smart as the grid you put in front of it.

Mechanics

How it works

  • Pick the hyperparameters and candidate values: n_values per knob.
  • Grid = ∏ n_values combinations (multiply every knob's list length).
  • For each combo, run k-fold CV and record mean + spread of the score.
  • Select the config with the best mean score (ties → smallest model / fastest train).
  • Refit that config on all the data (or keep the best fold model) for deployment.

Grid search over two knobs.

Knob A
Knob B
Grid
CV each
Winner
Real world

In production

  • A pricing model grid-searches 5 learning rates × 4 tree depths × 2 col_sample ratios on a small dataset — 40 CV fits finish in minutes.
  • CI runs a minimal grid each night so default-config regressions surface before the weekly full-scale search.
  • A feature-engineering team locks the data once, grids a model family to establish the baseline score everything else must beat.
Guidelines

Best practices

  • Use logarithmic or geometric spacing for magnitude-like knobs (learning rate 1e-4..1e-1, not 0.01, 0.02, 0.03).
  • Start coarse (2–3 values per knob) to find the region, then zoom in.
  • Always cross-validate each grid cell — a single split is noisy enough to misrank close cells.
  • Record the full result table, not just the winner; examining the surface explains why.
Watch out

Common mistakes

  • Making the grid enormous (10×10×10×10) and 'letting the cloud sort it out' — each failed run costs the same as successful ones.
  • Gridding fine steps over a log-scale knob, wasting cells on under-visited magnitude regimes.
  • Judging by best_score_ alone — adjacent cells often tie within the fold spread.
  • Retyping cumbersome scoring/refit logic instead of using GridSearchCV's cv_results_ properly.
Root cause analysis

Common issues

Error
Grid search finished but every cell scored nearly the same
Root cause

Grid too coarse or the knobs don't matter at this data size

Fix

Widen the value ranges or drop non-effective knobs and search the ones that move the metric.

Error
Best config on the grid loses to a hand-picked default
Root cause

Grid missed the good region entirely (too narrow/odd spacing)

Fix

Zoom out with log-spaced ranges and confirm the optimum is interior, not on a boundary.

When things break

Debugging tips

  • Inspect cv_results_: if rank-1 and rank-10 cells are within fold noise, shrink the grid or reduce fold variance.
  • Warm-start or parameterize model training so failed cells fail fast instead of wasting your whole budget.
  • Plot mean_test_score heat-maps for two-knob grids to see ridges and plateaus.
  • Confirm refit=True gives the deployed model the exact best config and no late copy-paste drift.
Interviews

Interview checkpoint

Practice

Hands-on lab

Grid Search, Sized to Survive
25 min · intermediate
In progress
Objectives
  • Run GridSearchCV and read cv_results_
  • Count the true cost (cells × folds × train time)
  • Discover a plateau where tuning stops mattering

Grid Search, Sized to Survive

25 min · intermediate

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

A grid with 4 knobs at 6 values each contains how many combinations?

2

Grid search is always better than random search because it is exhaustive.

Go deeper

Official documentation