← chapter

Random forests

Week 3's tree, bagged and de-correlated.

Grow many trees on bootstrap samples, split from random feature subsets, and let them vote. The variance that made one tree fragile averages out.

Two cheap tricks

The data

Two interleaving half-moons — a curved boundary a single tree can only approximate with a staircase.

The math

Average BB trees, each variance σ2\sigma^2, pairwise correlation ρ\rho:

Var=ρσ2+1ρBσ2\operatorname{Var} = \rho\,\sigma^{2} + \frac{1 - \rho}{B}\,\sigma^{2}

Adding trees kills the second term. Feature subsampling lowers ρ\rho, which lowers the floor ρσ2\rho\sigma^2. Predict by majority vote:

y^(x)=argmaxcb1 ⁣[Tb(x)=c]\hat{y}(x) = \arg\max_{c} \sum_{b} \mathbb{1}\!\left[T_b(x) = c\right]

OOB: ~36.8% of rows sit out each tree — a free held-out estimate.

Watch the boundary smooth

One tree per frame. The jagged single-tree staircase blurs into a smooth consensus; accuracy climbs then plateaus. Variance reduction, drawn in data.

Jagged trees, smooth forest

Three individual trees, each overconfident and wrong in different places. Average them and the jags cancel.

Scratch vs. library

RandomForestClassifier(n_estimators=60, max_depth=8) scores 0.740; our scratch forest 0.758 — within two points. One tree alone: 0.667. Bagging it 60 times bought nine points, no tuning.

Takeaways