Foundations to Frontier
Tests
D. Classical Machine Learning › D7 Ensemble Methods

Random Forests (Classification and Regression)

A random forest is bagging with one extra source of randomness: at every split, a tree may choose only among a random subset of m_try features. Bagged trees tend to make the same strong splits and so stay correlated. Restricting the candidates decorrelates them, and averaging decorrelated trees removes more variance. This page covers the algorithm, Breiman's strength-correlation bound, the m_try defaults (√p for classification, p/3 for regression), out-of-bag error, feature importance (impurity-based versus permutation, and the bias of the former), and practical tuning. In the exercises you build a forest with OOB error and both importance measures; then you measure how m_try trades tree strength against tree correlation on sparse and dense problems.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Bagging, but make the trees disagree

Bagged trees all see roughly the same data, so they usually pick the same strongest feature for their first split and end up alike. Averaging near-copies helps less than averaging genuinely different opinions. A random forest forces variety: at each split a tree may consider only a few randomly chosen features. Each tree gets a bit worse, but the trees become much less alike, and their vote becomes much better.

Random forest: many different trees, one vote
test accuracy: one tree 86.9% · forest of 25 93.6% · mean correlation between trees 0.69
Dashed: the true boundary. One fully grown tree carves boxes around every flipped label; the forest's vote is far smoother. Restricting each split to a random feature lowers the correlation between trees, so their errors cancel better. In 2-D it can also weaken each tree, so the net effect is small here; with many features it matters more.
Why engineers care

Random forests are the default first model for tabular data. They need little tuning, handle mixed feature types and interactions, rarely overfit badly as trees are added, give an out-of-bag error estimate for free, and rank feature importance. They are widely used for telemetry classification, remote-sensing land-cover maps and predictive maintenance.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A random forest adds to bagging…
  2. 2. Restricting features at each split makes the trees…
  3. 3. In the demo, one tree compared with the forest is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Decorrelation by hand

Variance of the average. For B trees with error variance σ² and pairwise correlation ρ, the forest’s variance is ρσ² + (1 − ρ)σ²/B. Take σ² = 1 and many trees:

  • bagging (ρ = 0.6): variance → 0.6;
  • random forest (ρ = 0.3): variance → 0.3, if each tree’s σ² stays similar.

In practice the trees get somewhat weaker (σ² and bias rise), which is the trade-off m_try controls.

How often does a tree see a feature? With p = 20 features and m_try = 4, a given feature is a candidate at a split with probability 4/20 = 0.2.

  • If only 3 features are informative, the chance that none of them is among the 4 candidates is (17·16·15·14)/(20·19·18·17) = 0.491.
  • Almost half the splits must then choose among noise features, which weakens the trees. That is why sparse problems prefer larger m_try (exercise 2).

Defaults. m_try = √p for classification (4 for p = 16) and p/3 for regression (10 for p = 30). Trees are grown deep, with no pruning, and minimum leaf sizes of 1 (classification) or 5 (regression).

The steps in plain English:

  1. For b = 1..B: draw a bootstrap sample. Grow a deep tree where each split considers only m_try randomly chosen features.
  2. Predict by majority vote, or averaged class probabilities, or the mean for regression.
  3. Estimate the error out of bag. Rank features by permutation importance.
  4. Tune m_try (and minimum leaf size) with the OOB error, and use enough trees for the OOB error to plateau.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Default m_try for classification with p = 25?
  2. 2. Probability a given feature is a candidate with p = 20, m_try = 4?
  3. 3. Limiting forest variance for σ² = 1, ρ = 0.3?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

The algorithm, OOB and importance

Breiman’s bound (2001). Let s be the “strength” (expected margin of the forest) and ρ̄ the mean correlation between trees’ raw margins. The forest’s generalisation error is at most PE* ≤ ρ̄·(1 − s²)/s². Better forests need strong and uncorrelated trees. Smaller m_try lowers ρ̄ but also s, and the optimum depends on how many features are informative.

Regression forests average tree predictions. Their error approaches ρ̄·(average tree error) as the number of trees grows.

Out-of-bag error. Each point is predicted by the roughly 37% of trees that did not see it. It is a nearly unbiased test-error estimate, used to tune m_try and the leaf size.

Feature importance.

  • MDI (mean decrease in impurity): the total weighted impurity drop from splits on each feature. It is cheap but biased toward features with many split points (continuous or high-cardinality) and computed on training data (Strobl et al., 2007).
  • Permutation importance: the drop in OOB or test accuracy when a feature’s values are shuffled. It is less biased, but it splits importance among correlated features.
  • Conditional permutation and SHAP values refine these further.

Extensions. Quantile regression forests (Meinshausen, 2006) give prediction intervals from leaf distributions. Proximities give similarity, imputation and outlier scores. Survival forests, causal forests (Wager and Athey, 2018) and isolation forest (for anomalies) build on the same machinery.

from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
rf = RandomForestClassifier(n_estimators=500, max_features="sqrt", oob_score=True, n_jobs=-1).fit(X, y)
rf.oob_score_; permutation_importance(rf, X_val, y_val, n_repeats=10)
🐍 Exercise 1: a random forest with OOB error and two importance measures
Implement a Gini tree with random feature subsets per split, forest fitting on bootstrap samples, OOB accuracy, MDI importance and permutation importance; verify that feature subsets reduce tree correlation, that OOB tracks test accuracy, that permutation importance finds the informative features, and that MDI favours a continuous noise feature over a binary one.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Breiman's bound says forest error grows with…
  2. 2. MDI importance is biased toward…
  3. 3. Permutation importance shuffles…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Consistency. Simplified random forests (purely random or median splits) are consistent (Biau, Devroye and Lugosi, 2008). Scornet, Biau and Vert (2015) proved consistency of Breiman’s original algorithm for additive regression models. Forests adapt to sparsity: their rate depends on the number of informative features, not on p (Biau, 2012).

Why forests rarely overfit as B grows. Adding trees only reduces the Monte Carlo variance of the average. Overfitting is controlled by tree depth, leaf size and m_try, not by B. Very deep trees on very noisy data can still overfit; a larger minimum leaf size helps.

The m_try trade-off (exercise 2).

  • With 3 strong features among 20, small m_try often leaves no informative feature among the candidates, so trees are weak. Accuracy rose from 0.74 (m_try = 1) to about 0.85 (8–20).
  • With 15 weak features, decorrelation won: m_try = 2 reached 0.748 against 0.712 for bagging.

Importance pitfalls. MDI rewards continuous noise (exercise 1: 0.096 for continuous noise against 0.014 for binary noise). Permutation importance spreads credit across correlated features and can be misleading when features are strongly dependent (it creates unrealistic combinations).

Interview questions.

  • How would you tune a random forest? Use enough trees (OOB error plateaus), tune m_try on the OOB error (try √p/2, √p, 2√p, p/3), raise min_samples_leaf for noisy targets, and use class weights for imbalance.
  • Why prefer permutation importance over the default feature_importances_? The default is MDI on training data, biased toward high-cardinality and continuous features. Permutation importance on held-out data measures predictive value.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Adding more trees to a forest…
  2. 2. With few informative features among many, m_try should be…
  3. 3. Forests adapt to sparsity: their rate depends on…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

The m_try trade-off

History.

  • Ho (1995, 1998) introduced random decision forests and the random-subspace method.
  • Amit and Geman (1997) used randomised trees for shape recognition.
  • Breiman (2001) defined random forests (bagging + random split candidates), the strength-correlation bound, OOB error and permutation importance.
  • Strobl et al. (2007) exposed the bias of impurity importance.
  • Wager and Athey (2018) introduced causal forests, and Scornet, Biau and Vert (2015) proved consistency.

Exercise 1.

  • RF with m_try = 2 reached 0.881 test accuracy against 0.894 for bagging (m_try = 6). With only 3 informative features of 6, decorrelation did not pay off here.
  • The mean correlation between trees fell from 0.63 to 0.49, and OOB accuracy was 0.897.
  • Permutation importance put x₀ (0.217), x₁ (0.228) and x₂ (0.069) on top, with noise near 0.
  • MDI gave the continuous noise feature 0.096 and the binary noise feature 0.014.

Exercise 2. Setup: 20 features, B = 25 trees, n = 300, 2 repetitions. Each cell shows forest accuracy / average single-tree accuracy / mean tree correlation.

m_try sparse (3 strong of 20) dense (15 weak of 20)
1 0.740 / 0.585 / 0.06 0.696 / 0.572 / 0.06
2 0.766 / 0.625 / 0.13 0.748 / 0.592 / 0.08
4 0.823 / 0.679 / 0.22 0.730 / 0.599 / 0.10
8 0.848 / 0.721 / 0.32 0.731 / 0.608 / 0.14
20 (bagging) 0.847 / 0.760 / 0.47 0.712 / 0.610 / 0.18
  • Larger m_try always gave stronger but more correlated trees, exactly Breiman’s trade-off.
  • On the sparse problem, strength mattered. The informative features must be available at each split, so m_try of 8–20 won. The √p default of 4 was 2.5 points short.
  • On the dense problem, decorrelation mattered, and bagging was worst. The results are noisy (2 repetitions).
  • Every forest beat its average tree by 13–16 points.
  • Lesson: tune m_try with the OOB error instead of trusting √p.
🐍 Exercise 2 (research): m_try, strength and correlation
On a sparse problem (3 strong features of 20) and a dense one (15 weak of 20), grow forests with m_try in {1, 2, 4, 8, 20}; report forest accuracy, average single-tree accuracy and mean correlation between trees.

Random forests in AI, robotics and aerospace

  • Remote sensing: land-cover and crop maps from multispectral imagery, a classic forest application.
  • Telemetry classification and anomaly triage with OOB-validated models.
  • Terrain classification for rovers from engineered visual and proprioceptive features.
  • Quantile regression forests for prediction intervals on loads and wear.

References

  1. L. Breiman (2001). “Random forests.” Machine Learning 45(1).
  2. T. K. Ho (1998). “The random subspace method for constructing decision forests.” IEEE TPAMI 20(8).
  3. C. Strobl, A.-L. Boulesteix, A. Zeileis, T. Hothorn (2007). “Bias in random forest variable importance measures.” BMC Bioinformatics 8.
  4. E. Scornet, G. Biau, J.-P. Vert (2015). “Consistency of random forests.” Annals of Statistics 43(4).
  5. N. Meinshausen (2006). “Quantile regression forests.” JMLR 7.