Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Machine Learning Notes
Topic #1902

Grid Search

Grid search exhaustively tries every combination of specified hyperparameter values — thorough and simple to reason about, but its cost grows multiplicatively with every added dimension.

Formula — Total Cost

\[ \text{Total model fits} = \left(\prod_{i=1}^{d} n_i\right) \times k \]

\(n_i\) is the number of values specified for hyperparameter \(i\), \(d\) is the number of hyperparameters being tuned, and \(k\) is the number of cross-validation folds.

Worked Example

Tuning an SVM's \(C\) (3 values) and \(\gamma\) (2 values) with 5-fold cross-validation:

\[ \text{Total fits} = (3 \times 2) \times 5 = 30 \]
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC

param_grid = {"C": [0.1, 1, 10], "gamma": [0.01, 0.1]}
search = GridSearchCV(SVC(), param_grid, cv=5, scoring="accuracy", verbose=1)
search.fit(X_train, y_train)
# scikit-learn will report "Fitting 5 folds for each of 6 candidates, totalling 30 fits" --
# exactly matching the hand calculation

How Fast This Explodes

HyperparametersValues EachCombinationsTotal Fits (5-fold CV)
23, 2630
33, 2, 424120
43, 2, 4, 5120600

This multiplicative growth — the "curse of dimensionality" applied to search, not just distance — is exactly why grid search becomes impractical past 3-4 hyperparameters, and why random search or Bayesian optimization take over for larger search spaces.

Inspecting Full Results

import pandas as pd

results = pd.DataFrame(search.cv_results_)
print(results[["params", "mean_test_score", "std_test_score"]].sort_values("mean_test_score", ascending=False))

Practical Use Cases

  • Small search spaces (1-3 hyperparameters, few values each), where exhaustive coverage is affordable
  • Final, focused fine-tuning around a promising region already identified by a coarser search

Advantages

  • Guaranteed to find the best combination within the specified grid
  • Simple, deterministic, and easy to explain

Limitations

  • Computational cost grows multiplicatively with every added hyperparameter
  • Wastes effort on unpromising regions of the search space just as much as promising ones
  • Only as good as the specific values you chose to include in the grid — a good setting between two grid points will never be found

Common Mistakes

  • Specifying an overly fine-grained grid on many hyperparameters simultaneously, causing an impractically long search.
  • Not checking whether the best result landed at the edge of the specified grid — if so, the true optimum may lie outside the tested range entirely.

Interview Relevance

Q: "Why does grid search become impractical with many hyperparameters?" Its cost grows as the product of the number of values per hyperparameter — adding even one more hyperparameter with a few values multiplies the total search cost, quickly becoming computationally infeasible, unlike random search's cost, which grows only with the number of iterations you choose to run.

Practice Question

You want to tune 3 hyperparameters with 4, 3, and 5 values respectively, using 10-fold cross-validation. How many total model fits does grid search require?

Related ML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →