# Bagging and Random Forests

> Averaging many decorrelated trees to cut variance, giving a robust, low-tuning model for tabular data. Free notes, key concepts, resources and a mini project.

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

Averaging many decorrelated trees to cut variance, giving a robust, low-tuning model for tabular data.

Intermediate · about 5 hours · difficulty 2/5

Learn first: [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)

A single tree is high variance: small data changes produce a very different tree. **Bagging** trains many trees on bootstrap samples and averages them. **Random forests** add random feature subsets at each split so trees disagree more, which makes the average even better.

### Key hyperparameters

- `n_estimators`: more trees rarely hurt, just cost time; a few hundred is typical.
- `max_features`: features considered per split; the main decorrelation knob.
- `max_depth` / `min_samples_leaf`: usually left fairly deep.
- `n_jobs=-1` to train in parallel.

### Useful extras

- **Out-of-bag (OOB) score**: each tree ignores about a third of the data, giving a free validation estimate.
- **Extra Trees**: even more randomized splits, faster and sometimes better.
- **Permutation importance** for trustworthy feature rankings.

### When to use

Random forests are an excellent low-effort baseline: hard to badly misconfigure, robust to outliers and irrelevant features. They are usually beaten by well-tuned gradient boosting, and they cannot extrapolate beyond the target range seen in training.

**Good enough to move on** when you can explain why averaging decorrelated trees reduces variance and use the OOB score to sanity-check a model.

## Key concepts

- **Bootstrap aggregation**
- **Random feature subsets**
- **Out-of-bag evaluation**
- **Permutation importance**
- **Extra Trees**

## Mini project

Train a random forest on the Kaggle House Prices dataset, compare OOB score with 5-fold CV score, and compare impurity-based and permutation feature importances.

## Check yourself

Why does restricting max_features at each split usually improve a forest even though each tree gets worse?

## Free resources

- [scikit-learn: Ensemble methods](https://scikit-learn.org/stable/modules/ensemble.html) (scikit-learn)
- [scikit-learn: Permutation feature importance](https://scikit-learn.org/stable/modules/permutation_importance.html) (scikit-learn)

Previous: [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees) · Next: [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)

## More in Classical Supervised Learning

- [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)
- [Linear Regression and Regularization](https://www.internhack.in/roadmaps/tracks/machine-learning/linear-regression)
- [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)
- [k-Nearest Neighbours and Naive Bayes](https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes)
- [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)
- [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests)
- [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)
- [Support Vector Machines and Kernels](https://www.internhack.in/roadmaps/tracks/machine-learning/support-vector-machines)

## Related

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