Skip to main content

Hyperparameter Tuning

Grid Search vs Random Search

Parameters vs Hyperparameters

Parameters: Learned from data during training
  • Weights in linear regression
  • Split points in decision trees
Hyperparameters: Set before training
  • Learning rate
  • Number of trees in Random Forest
  • Maximum depth of trees
You choose hyperparameters. The model learns parameters. Think of it like baking a cake. The parameters are the exact mixing ratios the recipe produces (flour, sugar, eggs). The hyperparameters are the oven temperature and baking time — you set them before baking starts, and they control how the recipe turns out. You can’t learn the oven temperature from the batter; you have to experiment.
YouTube Recommendation Tuning

The Tuning Problem

A Random Forest has many hyperparameters:
How do you find the best combination?

Grid Search: Try Everything

Grid search is the brute-force approach: define a grid of values and try every single combination. It’s like testing every possible combination of oven temperature and baking time — thorough but expensive. For 3 hyperparameters with 4 values each, that’s 4 x 4 x 4 = 64 combinations, each requiring a full cross-validation run.

Visualizing Grid Search Results


Random Search: Smart Sampling

Grid search has a problem: exponential explosion.
  • 5 hyperparameters
  • 5 values each
  • 5^5 = 3,125 combinations!
Here’s the key insight from Bergstra and Bengio’s 2012 paper: in most ML problems, only 1-2 hyperparameters actually matter. Grid search wastes most of its budget exhaustively varying the ones that don’t matter. Random search, by contrast, samples each important dimension more thoroughly. Think of it like searching for a lost key in a field: grid search mows the lawn in neat rows, while random search drops random probes — if the key is somewhere along a specific line, random search is more likely to hit that line. Random Search samples randomly from parameter distributions:
Research shows (Bergstra & Bengio, 2012): Random search often finds good hyperparameters faster than grid search. The intuition is simple — if only 2 of your 5 hyperparameters actually matter (which is common), grid search wastes most of its budget exploring irrelevant dimensions. Random search spreads trials across all dimensions, so you explore more unique values of the important parameters with the same compute budget.Practical rule: Use grid search when you have 2-3 hyperparameters with known good ranges. Use random search when you have 4+ hyperparameters or wide, uncertain ranges.

Bayesian Optimization: Learn from History

Grid search ignores past results. Random search ignores past results. Bayesian optimization is smarter — it builds a model of “which hyperparameters lead to good scores” and uses that model to decide where to look next. Think of it like a gold prospector who, after finding gold in one spot, digs nearby rather than randomly across the entire mountain.

How Bayesian Optimization Works

  1. Try some random points
  2. Build a model of: parameter values → score
  3. Use model to find promising regions
  4. Evaluate and update model
  5. Repeat
Balances exploration (try new areas) and exploitation (focus on promising areas).

Optuna: Modern Hyperparameter Tuning


Practical Tips

1. Start Coarse, Then Refine

2. Prioritize the Most Impactful Hyperparameters

Not all hyperparameters are created equal. Tune the ones that move the needle most, and leave the rest at sensible defaults.

3. Different Metrics for Different Problems

4. Nested Cross-Validation

For unbiased evaluation of the tuning process. This is subtle but important: regular cross-validation with hyperparameter tuning gives you an optimistically biased estimate of performance. You picked the best hyperparameters on the same folds you’re reporting results for. Nested CV fixes this by using separate inner folds for tuning and outer folds for evaluation.

Common Hyperparameters by Model

Common ML mistake — tuning before feature engineering: Hyperparameter tuning typically yields 1-3% improvement. Good feature engineering yields 5-20%. Always get your features right first, then tune. A perfectly tuned model on bad features will lose to a default model on great features every time.

Random Forest

Gradient Boosting / XGBoost

SVM

Neural Networks


🚀 Mini Projects

Project 1: Search Strategy Comparison

Compare Grid, Random, and Bayesian search

Project 2: Learning Curve Analyzer

Diagnose underfitting vs overfitting with tuning

Project 3: Custom Hyperparameter Optimizer

Build your own optimization algorithm

Project 4: Auto-ML Mini Framework

Create an automated model tuning system

Project 1: Search Strategy Comparison

Compare different hyperparameter search strategies on the same problem.

Project 2: Learning Curve Analyzer

Use learning curves to determine if more data or different hyperparameters would help.

Project 3: Custom Hyperparameter Optimizer

Build a simple Bayesian-style optimizer from scratch.

Project 4: Auto-ML Mini Framework

Create an automated model tuning system that handles multiple models.

Key Takeaways

Grid Search

Exhaustive but slow. Good for small spaces.

Random Search

Often better than grid. Use for larger spaces.

Bayesian Optimization

Smart search. Best for expensive evaluations.

Nested CV

Unbiased estimate of tuning performance.

What’s Next?

You’ve learned individual algorithms. Now let’s see how to tackle real-world ML projects end-to-end!

Continue to Module 10: End-to-End ML Project

Apply everything in a complete machine learning project