Random Forests

A Random Forest is an ensemble learning method that builds many decision trees during training and merges their predictions to produce a more accurate and stable result than any single tree could achieve. It matters enormously in modern AI because it remains one of the most reliable out-of-the-box algorithms for tabular data, often competitive with gradient boosting and deep learning with far less tuning. The counterintuitive fact: making each tree worse on purpose — by limiting which features it can see — actually makes the overall forest better.

The core insight

The magic of Random Forests is that diversity among imperfect models beats perfection in a single model. Each tree is deliberately trained on a random subset of the data and a random subset of the features, making it somewhat wrong in different ways. When you average hundreds of these "usefully wrong" trees together, their errors cancel out while their correct signals reinforce each other. This is the ensemble principle: a committee of diverse, mediocre experts consistently outperforms one overconfident genius.

How It Works

Random Forests combine two core ideas: bagging (bootstrap aggregating) and random feature selection. Here is the full recipe, step by step.

Step 1 — Bootstrap Sampling

For each tree tt out of TT total trees, draw a bootstrap sample: randomly pick nn rows from your training set with replacement. About 63% of original rows appear at least once; the rest become the "out-of-bag" (OOB) sample, which gives you a free validation set.

1
y^=1T∑t=1Tft(x)\quad \hat{y} = \frac{1}{T} \sum_{t=1}^{T} f_t(x)

Explanation: The forest's prediction is simply the average (for regression) of all T individual tree predictions for input x.

  • y^\hat{y} — the final predicted value
  • TT — total number of trees in the forest
  • ft(x)f_t(x) — the prediction of the tt-th tree for input xx

For classification, replace the average with a majority vote across all trees.

Step 2 — Random Feature Selection at Each Split

When growing each tree, at every node split the algorithm does not consider all features. Instead it randomly samples mm features out of the total pp available, then picks the best split among only those mm.

2
m≈p\quad m \approx \sqrt{p}

Explanation: A common rule of thumb sets the number of candidate features per split to the square root of the total number of features, which balances diversity and informativeness.

  • mm — number of features randomly sampled at each split
  • pp — total number of features in the dataset

For regression tasks, m≈p/3m \approx p/3 is often preferred. In scikit-learn this is the `max_features` parameter.

Step 3 — Grow Trees Without Pruning

Each tree is grown deep (or to a specified `max_depth`) on its bootstrap sample using only mm features per split. The trees are high-variance but low-bias — exactly the kind of model that benefits from averaging.

Step 4 — Aggregate Predictions

Collect all TT predictions and average them (regression) or vote (classification). The out-of-bag rows — the ones not used to train tree tt — give a built-in accuracy estimate called the OOB score, no separate validation set needed.

3
OOB Score=1n∑i=1n1[y^iOOB=yi]\quad \text{OOB Score} = \frac{1}{n} \sum_{i=1}^{n} \mathbf{1}\left[\hat{y}_i^{OOB} = y_i\right]

Explanation: The OOB score is the fraction of training samples correctly predicted by the trees that did NOT train on that sample — an honest estimate of generalization accuracy.

  • nn — number of training samples
  • y^iOOB\hat{y}_i^{OOB} — prediction for sample ii using only trees that did not see it during training
  • yiy_i — true label of sample ii
  • 1[⋅]\mathbf{1}[\cdot] — the indicator function (1 if condition is true, 0 otherwise)

Feature Importance

Random Forests provide a useful bonus: Gini importance (also called mean decrease in impurity). For each feature, the algorithm sums how much each split using that feature reduced impurity (e.g., Gini index or MSE), averaged across all trees. Higher total reduction = more important feature. ```python import numpy as np from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import load_iris import matplotlib.pyplot as plt

# Load example data X, y = load_iris(return_X_y=True) feature_names = load_iris().feature_names

# Train a Random Forest rf = RandomForestClassifier( n_estimators=200, # number of trees max_features="sqrt", # random feature subset per split max_depth=None, # grow trees fully oob_score=True, # use out-of-bag samples for free validation random_state=42 ) rf.fit(X, y) print(f"OOB Accuracy: {rf.oob_score_:.3f}")

# Feature importances importances = rf.feature_importances_ for name, imp in zip(feature_names, importances): print(f" {name}: {imp:.3f}") ``` ``` OOB Accuracy: 0.953 sepal length (cm): 0.093 sepal width (cm): 0.025 petal length (cm): 0.441 petal width (cm): 0.441 ``` Notice how you get a free accuracy estimate (`oob_score_`) without touching a test set at all.

Quick Tuning Guide

```python from sklearn.model_selection import cross_val_score

# The three most impactful hyperparameters to tune: rf_tuned = RandomForestClassifier( n_estimators=500, # More trees = more stable, plateau usually around 200-500 max_depth=10, # Limit depth to reduce overfitting on noisy data min_samples_leaf=5, # Require at least 5 samples at leaves — smooths predictions max_features="sqrt", # Default is fine for classification n_jobs=-1, # Use all CPU cores — forests parallelize perfectly random_state=42 ) scores = cross_val_score(rf_tuned, X, y, cv=5, scoring="accuracy") print(f"CV Accuracy: {scores.mean():.3f} ± {scores.std():.3f}") ```

researcher · Leo Breiman | 1928 – 2005 | Invented Random Forests

Leo Breiman was a statistician at UC Berkeley who spent decades bridging theoretical statistics and practical machine learning, coining the term "algorithmic modeling" to describe the data-first philosophy he championed. In 2001 he published the landmark paper Random Forests, combining his earlier work on bagging (1996) with Tin Kam Ho's random subspace method to create the algorithm as we know it today. Remarkably, Breiman was a former professional poker player — a hobby that perhaps sharpened his intuition for how randomness, managed carefully, reduces risk.

researcher · Tin Kam Ho | b. 1963 | Random Subspace Method

Tin Kam Ho is a researcher at Bell Labs (now Nokia Bell Labs) who introduced the random subspace method in 1995 — the critical idea of randomly selecting feature subsets at each tree, which became the distinguishing ingredient of Random Forests. Her work showed that ensembles of deliberately "crippled" decision trees could dramatically outperform single optimal trees, a result that seemed paradoxical at the time. Ho's contribution is often under-credited in popular accounts, which focus almost entirely on Breiman's 2001 paper.

Figure
Random Forest Architecture: From Data to Prediction
Random Forest: Bootstrap → Trees → Aggregate Training Data n rows × p cols Bootstrap Sample 1 Bootstrap Sample 2 ⋮ Bootstrap Sample T √p features per split Tree 1 Tree 2 ⋮ Tree T Aggregate Avg (regression) Vote (classify) Prediction ŷ ~37% rows unused → free OOB score
Figure
Feature Importance: What Each Feature Contributes
Feature Importance (Mean Decrease in Impurity) petal length 0.441 petal width 0.441 sepal length 0.093 sepal width 0.025 0.0 0.5 1.0 Importance Score (sums to 1.0)

Intuition

The Mental Model

Imagine you want to predict whether a new restaurant will succeed. You could ask one very experienced food critic — but they have strong personal biases and blind spots. Instead, you survey 500 ordinary diners, each of whom has eaten at a different random sample of restaurants and considers only a few factors (price, atmosphere, food quality) when rating each one. No single diner's judgment is reliable, but when you tally all 500 votes, the quirks and errors average away and the genuine signal — "does the food taste good and is the price fair?" — emerges clearly. This is exactly what a Random Forest does. Each tree is a diner: trained on a slightly different set of experiences (bootstrap sample), looking at a random subset of factors (feature subspace), and delivering an imperfect judgment. The forest is the wisdom of the crowd — robust, reliable, and self-calibrating through the OOB score.

Historical Development

Origins · 1 minhow this was discovered
History
1984
Leo Breiman, Friedman, Olshen, and Stone publish Classification and Regression Trees (CART), establishing the foundational decision tree algorithm that Random Forests would later use as its building block.
1994
Robert Schapire formalizes the theory of boosting, demonstrating that weak learners can be combined into strong ones — the theoretical cousin of bagging that sparks widespread interest in ensemble methods.
1995
Tin Kam Ho introduces the random subspace method (also called "forest of random decision trees") at ICDAR, the first algorithm to build trees using random feature subsets — the key ingredient missing from bagging alone.
1996
Leo Breiman introduces bagging (bootstrap aggregating), showing that averaging predictions from models trained on bootstrap samples of data significantly reduces variance, especially for unstable learners like decision trees.
1997
Amit and Geman independently propose growing trees using random feature selection for image segmentation, converging on a similar idea to Ho's from a different research community.
2001
Leo Breiman publishes "Random Forests" in Machine Learning journal, the landmark paper that formally names and unifies the algorithm — combining bagging with random feature subsets — and proves convergence and generalization bounds.
2006
Random Forests become a core tool in the Netflix Prize competition and bioinformatics, demonstrating strong performance on high-dimensional data with many irrelevant features (e.g., gene expression datasets with thousands of variables).
2010
scikit-learn ships `RandomForestClassifier` and `RandomForestRegressor` in Python, democratizing access to the algorithm and making it one of the most widely used ML tools ever written.
2016
Random Forests remain the most-used algorithm in Kaggle competitions according to a major survey, outperformed only by gradient boosted trees (XGBoost) — illustrating both their power and their natural successor.

Why It Matters

Real-World Impact
Credit Scoring (Banking)FICO and many banks use Random Forests to assess loan default risk — the algorithm's natural feature importance ranking helps risk officers explain which factors (debt-to-income ratio, payment history) drove a decision, satisfying regulatory explainability requirements that black-box models cannot meet.
Medical DiagnosisThe landmark 2006 study by Svetnik et al. showed Random Forests outperforming neural networks on drug activity prediction (QSAR datasets). Today they are used in clinical decision support systems to flag sepsis risk in ICUs, where reliability matters more than squeezing out every last percent of accuracy.
Remote Sensing & Land ClassificationNASA and ESA use Random Forests to classify satellite imagery pixels into land cover types (forest, urban, water) — a task with millions of samples and hundreds of spectral features where the algorithm's parallelism and OOB validation make it uniquely practical.
Fraud Detection (Real-time)PayPal and major credit card processors use Random Forest variants in real-time fraud scoring pipelines. Each transaction is scored in milliseconds; the ensemble of trees evaluates thousands of behavioral features simultaneously and flags anomalies without requiring feature normalization.
Ecology & Species Distribution ModelingMaxEnt and Random Forest-based models predict where endangered species will be found given climate and terrain variables. The WHO used similar approaches during the 2014 Ebola outbreak to predict geographic spread risk across West Africa.
Recommender Systems (Feature-Based)Spotify uses tree-based ensembles, including Random Forests, to model user listening patterns from audio features and behavioral signals — complementing collaborative filtering for cold-start users who have little listening history.
Kaggle Competitions & AutoMLRandom Forests are a default first-pass algorithm in AutoML systems like Google's AutoML Tables and H2O AutoML because they require minimal preprocessing (no scaling, handle missing values gracefully, no need to one-hot encode ordinal features) and produce competitive baselines within minutes.

Prerequisites

What This Leads To

See Also

Decision Trees · Gradient Boosted Trees · Bagging and Boosting · Bias-Variance Tradeoff · XGBoost and LightGBM

Before this