What Makes a Random Forest Random?
A Random Forest introduces randomness in two complementary ways during training: bagging (bootstrap aggregating) and random feature subsampling at each split. Bagging trains each decision tree on a distinct bootstrap sample drawn with replacement from the training set, while split-level feature randomness restricts each node split to a random subset of candidate features (such as sqrt(p) in classification). Together, these mechanisms decorrelate individual low-bias, high-variance trees so averaging their predictions sharply lowers ensemble variance.
Study Card: What Makes a Random Forest Random?
Direct Answer
The “random” in Random Forest comes from two distinct sources of stochasticity applied while growing an ensemble of decision trees:
- Bagging (Bootstrap Aggregating): Each tree is trained on a bootstrap sample of size drawn with replacement from the original training examples (covering on average unique examples per tree, leaving out-of-bag).
- Feature Randomness (Random Subspace at Each Split): Whenever a tree chooses a split at a node, it evaluates only a random subset of features out of all features (typically for classification and for regression).
Why is feature randomness critical beyond plain bagging? For identically distributed trees each with variance and pairwise correlation , the variance of the ensemble average is:
If one dominant feature drives every bagged tree’s top splits, remains high and variance reduction stalls at . Restricting split candidates forces trees to explore diverse feature combinations, lowering and markedly improving generalization.
Key Terms
- Bagging (Bootstrap Aggregating): Sampling training points with replacement per tree to inject data-level diversity.
- Out-of-Bag (OOB) Samples: The of points omitted from a tree’s bootstrap sample, useful for free internal validation (
oob_score=True). - Feature Subsampling (
max_features): Randomly selecting candidate features independently at every node split to decorrelate trees. - Decorrelation: Reducing pairwise prediction correlation between trees so ensemble averaging reduces variance further.
Example
Suppose a churn dataset has one ultra-predictive feature (account_age) alongside weaker behavioral signals (support_tickets, usage_drop, billing_failures). Under standard bagging without feature randomness, nearly every tree splits first on account_age, creating structurally similar, highly correlated trees. In a Random Forest with max_features='sqrt', roughly of root splits exclude account_age, allowing different trees to discover predictive rules across the remaining features and lowering the ensemble variance when predictions are averaged or voted.
Code Implementation
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.tree import export_text
X, y = make_classification(
n_samples=1000, n_features=10, n_informative=5, n_classes=2, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# bootstrap=True enables bagging; max_features="sqrt" enables per-split feature randomness
rf = RandomForestClassifier(
n_estimators=100,
max_features="sqrt",
bootstrap=True,
oob_score=True,
random_state=42,
)
rf.fit(X_train, y_train)
# 1. Inspect actual bootstrap sample indices used by the first tree
bagged_indices = rf.estimators_samples_[0]
unique_fraction = len(np.unique(bagged_indices)) / len(X_train)
print(f"Train size: {len(X_train)}, Tree 0 bootstrap draws: {len(bagged_indices)}")
print(f"Unique samples in Tree 0: {len(np.unique(bagged_indices))} ({unique_fraction:.1%}, theoretical ~63.2%)")
print(f"OOB score: {rf.oob_score_:.3f}, Test accuracy: {rf.score(X_test, y_test):.3f}")
# 2. Compare root split features across the first 5 trees (decorrelated splits)
root_features = [est.tree_.feature[0] for est in rf.estimators_[:5]]
print("Root split feature index for first 5 trees:", root_features)
tree_rules = export_text(rf.estimators_[0], feature_names=[f"feature_{i}" for i in range(10)])
print("\nTop of Tree 0 structure:\n", "\n".join(tree_rules.splitlines()[:6]))
Related Concepts
- Decision Trees: High-variance, low-bias base learners that benefit strongly from ensemble averaging.
- Extremely Randomized Trees (Extra-Trees): Adds a third source of randomness by picking split thresholds randomly rather than searching for optimal thresholds.
- Bagging vs. Boosting: Random Forests grow independent deep trees in parallel to reduce variance, whereas Gradient Boosting grows shallow trees sequentially to reduce bias.
- Hyperparameters:
n_estimatorslowers the term asymptotically without overfitting (though training cost grows linearly), whilemax_featurestrades individual tree strength against inter-tree correlation .