# Hyperparameter Tuning

> Grid, random and Bayesian search with tools like Optuna, plus how to tune efficiently without overfitting the validation set. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/hyperparameter-tuning
Last updated: 2026-11-28

Grid, random and Bayesian search with tools like Optuna, plus how to tune efficiently without overfitting the validation set.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Bias, Variance and Generalization](https://www.internhack.in/roadmaps/tracks/machine-learning/bias-variance), [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)

Hyperparameters are settings you choose rather than learn (tree depth, learning rate, regularization strength). Systematic tuning often gives a solid gain, but beyond a point it offers less than better data or features.

### Methods

- **Grid search**: exhaustive, fine for two or three parameters.
- **Random search**: usually more efficient than grid, because only a few parameters matter much.
- **Bayesian optimization** (Optuna, scikit-optimize): builds a model of the score surface and samples promising regions.
- **Successive halving / Hyperband**: kill bad configurations early to save compute.

### Practice

- Search learning rates and regularization on a **log scale**.
- Start from library defaults and published good ranges; tune the most important parameters first.
- Tune inside cross-validation with a pipeline, then evaluate the final choice on the untouched test set.
- Log every trial (value, parameters, time) so you can see which parameters matter.

### Pitfalls

Running thousands of trials against one small validation split effectively trains on it. Watch for improvements smaller than fold-to-fold noise.

**Good enough to move on** when you can run an Optuna study on a gradient boosting model with cross-validation and explain the parameter importance plot.

## Key concepts

- **Grid search**
- **Random search**
- **Bayesian optimization with Optuna**
- **Successive halving**
- **Log-scale search spaces**

## Mini project

Tune a LightGBM model on the Kaggle House Prices data with both RandomizedSearchCV and an Optuna study of equal budget, and compare best scores and wall-clock time.

## Check yourself

Why does random search often beat grid search with the same number of trials?

## Free resources

- [scikit-learn: Tuning hyperparameters](https://scikit-learn.org/stable/modules/grid_search.html) (scikit-learn)
- [Optuna documentation](https://optuna.readthedocs.io/en/stable/) (Optuna)

Previous: [Bias, Variance and Generalization](https://www.internhack.in/roadmaps/tracks/machine-learning/bias-variance) · Next: [Imbalanced Data](https://www.internhack.in/roadmaps/tracks/machine-learning/imbalanced-data)

## More in Evaluating, Tuning and Packaging Models

- [Evaluation Metrics](https://www.internhack.in/roadmaps/tracks/machine-learning/evaluation-metrics)
- [Cross-Validation](https://www.internhack.in/roadmaps/tracks/machine-learning/cross-validation)
- [Bias, Variance and Generalization](https://www.internhack.in/roadmaps/tracks/machine-learning/bias-variance)
- [Hyperparameter Tuning](https://www.internhack.in/roadmaps/tracks/machine-learning/hyperparameter-tuning)
- [Imbalanced Data](https://www.internhack.in/roadmaps/tracks/machine-learning/imbalanced-data)
- [scikit-learn Pipelines and Model Persistence](https://www.internhack.in/roadmaps/tracks/machine-learning/sklearn-pipelines)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
