Hyperparameter Tuning
Parameters vs Hyperparameters
Parameters: Learned from data during training- Weights in linear regression
- Split points in decision trees
- Learning rate
- Number of trees in Random Forest
- Maximum depth of trees
The Tuning Problem
A Random Forest has many hyperparameters: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!
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
- Try some random points
- Build a model of: parameter values → score
- Use model to find promising regions
- Evaluate and update model
- Repeat
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