Python Tutorial

Grid Search

Systematically search hyperparameter combinations to find the settings that generalize best.

Parameters vs Hyperparameters

A model learns its parameters (coefficients, split thresholds) from data. Its hyperparameters — like C, max_depth, or n_neighbors — are set before training and control how learning happens. Grid search tries every combination on a grid and keeps the best, judged by cross-validation.

Basic GridSearchCV

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV

X, y = load_iris(return_X_y=True)

param_grid = {
    "C": [0.01, 0.1, 1, 10, 100],
    "solver": ["lbfgs", "liblinear"],
}

grid = GridSearchCV(
    LogisticRegression(max_iter=1000),
    param_grid,
    cv=5,
    scoring="accuracy",
    n_jobs=-1)          # use all CPU cores

grid.fit(X, y)

print("Best params:", grid.best_params_)
print("Best CV score:", grid.best_score_.round(3))

After fitting, grid.best_estimator_ is the model retrained on the full data with the winning settings — ready to predict.

Searching Inside a Pipeline

Prefix parameter names with the step name (step__param) so preprocessing is re-fit inside each fold — the leak-free way to tune.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

pipe = Pipeline([("scaler", StandardScaler()), ("svc", SVC())])

param_grid = {
    "svc__C": [0.1, 1, 10],
    "svc__gamma": [0.01, 0.1, 1],
    "svc__kernel": ["rbf"],
}

grid = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1)
grid.fit(X, y)

Random Search for Large Spaces

Grid search cost explodes combinatorially. When the grid is huge, RandomizedSearchCV samples a fixed number of combinations and often finds near-optimal settings far faster.

from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import loguniform

param_dist = {"C": loguniform(1e-3, 1e3), "gamma": loguniform(1e-4, 1e0)}

search = RandomizedSearchCV(
    SVC(), param_dist, n_iter=25, cv=5, random_state=0, n_jobs=-1)
search.fit(X, y)

Inspecting All Results

import pandas as pd

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

Best Practices

  • Hold out a final test set; grid search should only ever see the training data.
  • Search hyperparameters on a log scale (0.01, 0.1, 1, 10, …).
  • Use RandomizedSearchCV or Bayesian optimization when the grid is large.
  • Set n_jobs=-1 to parallelize, and pick a scoring metric that matches your goal (e.g. f1 for imbalanced data).

Try It Yourself

Exercise 1: Grid-search C in [0.1, 1, 10] for logistic regression on iris and print the best value.

Show solution
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
X, y = load_iris(return_X_y=True)
g = GridSearchCV(LogisticRegression(max_iter=1000), {"C": [0.1, 1, 10]}, cv=5)
g.fit(X, y)
print(g.best_params_)

Exercise 2: When the search grid is huge, what should you use instead of exhaustive grid search?

Show solution

RandomizedSearchCV (or Bayesian optimization) — it samples combinations and finds near-optimal settings far faster.

📘 Real-World Deep Dive

Knowing <strong>ML Grid Search (scikit-learn)</strong> well is what turns scikit-learn from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of ML Grid Search that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV, train_test_split
X, y = load_iris(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, random_state=0)
grid = GridSearchCV(SVC(), {"C":[0.1, 1, 10]}, cv=3).fit(Xtr, ytr)
print("best:", grid.best_params_, "score:", round(grid.score(Xte, yte), 3))

Expected Output

(see source)

Common mistakes

  • fit expects numeric arrays; OneHotEncoder / LabelEncoder are easy to forget for categorical features.
  • Calling predict on a model trained on unscaled data and then scaling inputs at inference time silently degrades accuracy.
  • train_test_split(X, y) requires both arrays; train_test_split(X) for unsupervised learning slips past static checkers.
  • Treating ML Grid Search as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Wrap preprocessing + estimator in a Pipeline so fit / predict stay reproducible.
  • Use joblib / pickle for serialising models, not the entire Python state.
  • For > 100 k rows, switch to HistGradientBoostingClassifier or cuML.
  • When working with scikit-learn, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Grid Search

Common questions about this page.

What is Grid Search?

Grid Search is a Machine Learning lesson that explains grid search in Python. Systematically search hyperparameter combinations to find the settings that generalize best. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run grid search examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn grid search in this Machine Learning Python lesson (Grid Search).

How do I use grid search in Python?

To use grid search in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of grid search?

This Grid Search tutorial shows grid search syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Grid Search example for beginners

Yes. This page includes a beginner grid search example you can copy and run. It is designed for searches such as "grid search for beginners", "grid search example", and "how to use grid search".

What are common mistakes with grid search?

Common grid search mistakes include wrong syntax, mixing types, and skipping practice. Work through this Machine Learning chapter in order, run every example, and check the output before moving on.

Why should I learn grid search?

Grid Search is used in real Python work. Learning grid search helps you write clearer programs and continue the Machine Learning tutorial on StudyGrid.

Is Grid Search free to learn online?

Yes. You can learn grid search free on StudyGrid (studygrid.in). This chapter is part of the Machine Learning path and includes examples, syntax, and next-step links.