← Explainer Library

Interactive Explainer

Random Forests, Tree by Tree

Bagging + random feature sampling — the simplest tabular ensemble that still wins half the time. Add trees one at a time; watch the boundary smooth, OOB error fall, feature importance stabilise. Compare RF to bagging-only and Extra-Trees on the same dataset.

Prelude

The variance-reduction argument in two lines

A single deep tree is high-variance: small training perturbations move splits a lot. If we have $T$ trees with pairwise correlation $\rho$ and per-tree variance $\sigma^2$, the variance of the average prediction is

Two terms: an irreducible part driven by between-tree correlation, and a $1/T$ part driven by independence. Bagging lowers $\rho$ by training each tree on a different bootstrap sample. Random feature subsampling lowers $\rho$ further by forcing different trees to split on different features. The combination is what makes a random forest stable: $T \to \infty$ drives the $\sigma^2/T$ term to zero, and the residual $\rho\sigma^2$ is much smaller than $\sigma^2$.

A single deep tree is unbiased-ish but variance-heavy; averaging hundreds of them keeps the bias and crushes the variance. This is the entire intuition behind every bagged ensemble.

Step 1

Two sources of diversity

A single deep decision tree is high-variance — small training-set perturbations move the splits a lot. Random Forest (Breiman, 2001) averages many such trees, each de-correlated from the others by two tricks:

Extra-Trees (Geurts et al., 2006) goes further: at each split, pick the threshold randomly rather than optimally. Trades a little bias for even more variance reduction; often slightly worse on signal-rich data, slightly better on noisy data.

Step 2

Watch the forest grow

Click Add tree to add one tree at a time. The forest's predicted class is the majority vote of its trees. The boundary smooths quickly in the first 10 trees and gradually after that. OOB error is reported only once $\ge$ 3 trees exist.

trees 0 OOB error
Bagging only (m = d = 2)
Random Forest (m via slider)
Extra-Trees (random splits)
OOB error + feature importance, tree by tree
Step 3

OOB error — the free held-out set

Each training point is "out-of-bag" for the ~37% of trees whose bootstrap didn't include it. To get its OOB prediction: vote across just those trees. The OOB error is the misclassification rate over all training points using their respective OOB predictions.

Properties: (1) no leakage; (2) you don't have to split into train/test for tuning; (3) converges to the true test error as trees grow. The OOB curve in Step 2 should match the slope of a held-out test curve within noise.

Step 4

Feature importance — three ways, none perfect

MDI is what the live demo shows. Always cross-check with permutation importance on real datasets — MDI can flag a random-noise high-cardinality feature as "important".

Step 5

What RF is good at — and what trips it up

The slogan. Wisdom-of-crowds, but each crowd-member sees a different slice of the world. The math behind it is one line (averaging i.i.d. weak learners reduces variance by 1/n); the practical implication is that you should never train one decision tree when you can train fifty.
Step 6

RF vs Gradient Boosting — when to use which

AxisRandom ForestGradient Boosting (XGBoost / LightGBM)
TrainingTrees are independent (parallel)Trees are sequential (later trees fix earlier mistakes)
Per-tree depthDeep (often unlimited)Shallow (3–8)
Sensitivity to hyperparametersRobust — defaults usually workSensitive — must tune learning rate, max_depth, regulariser
Risk of overfitLow (averaging)Higher — need early stopping
CalibrationPoorly calibrated probabilitiesSlightly better, still wrap with Platt / isotonic
Best onMany features, mild signal, fast prototypingTight accuracy targets, large datasets, Kaggle

Mental model: RF averages many trained-from-scratch trees; boosting trains a sequence of small trees on each other's residuals. See the XGBoost article for boosting's mechanics.

Step 7

Hyperparameter cheat sheet

ParameterDefaultWhat to change & when
n_estimators100Increase until OOB error plateaus; cheap upside, no overfit.
max_depthNone (full)Cap at 10–15 if memory matters; small effect on accuracy.
max_features$\sqrt d$ (clf) / $d/3$ (reg)Lower for redundant features; raise for sparse / weak features.
min_samples_leaf1Raise to 5 if labels are noisy; smoother boundary.
bootstrapTrueSet False to disable bagging (then OOB is undefined).
class_weightNoneUse "balanced" for imbalanced classification.
n_jobs1-1 to use all CPU cores — embarrassingly parallel.
Step 8

Reading list