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 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 out of total trees, draw a bootstrap sample: randomly pick 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.
Explanation: The forest's prediction is simply the average (for regression) of all T individual tree predictions for input x.
- — the final predicted value
- — total number of trees in the forest
- — the prediction of the -th tree for input
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 features out of the total available, then picks the best split among only those .
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.
- — number of features randomly sampled at each split
- — total number of features in the dataset
For regression tasks, 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 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 predictions and average them (regression) or vote (classification). The out-of-bag rows — the ones not used to train tree — give a built-in accuracy estimate called the OOB score, no separate validation set needed.
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.
- — number of training samples
- — prediction for sample using only trees that did not see it during training
- — true label of sample
- — 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}") ```
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.
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.
Intuition
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
Why It Matters
Prerequisites
What This Leads To
See Also
Decision Trees · Gradient Boosted Trees · Bagging and Boosting · Bias-Variance Tradeoff · XGBoost and LightGBM