Sub topic 7
Random feature subsets decorrelate the trees
This chapter assumes you know how a decision tree picks splits with an impurity criterion, and that you understand bootstrap sampling and bagging. It explains why bagging alone leaves the trees too similar, and how random feature subsets fix that.
The problem: bagged trees are correlated
Bagging averages many trees, each trained on a different bootstrap sample. Averaging reduces variance only if the trees make different errors. If one feature is very strong, almost every tree picks it for the root split. Most trees then share the same top structure and make similar mistakes.
For B models, each with variance σ² and pairwise correlation ρ, the variance of the average is:
ρσ² + (1 − ρ)σ² / B
The second term shrinks as you add trees. The first term does not. Adding trees cannot push the error below ρσ², so the remaining lever is lowering ρ.
The fix: a random subset at every split
At each split, the tree does not search all features. It draws a random subset of m features and picks the best split among only those. A new subset is drawn at every node, not once per tree.
A strong feature is then missing from the candidate set at many nodes. The tree must use other features there, so different trees take different paths through the data.
Choosing m
The usual defaults are:
| Task | Common default for m | scikit-learn setting |
|---|---|---|
| Classification | √p | `max_features="sqrt"` |
| Regression | p / 3 | `max_features=0.33` |
In scikit-learn, RandomForestClassifier already defaults to "sqrt". RandomForestRegressor defaults to 1.0, which uses all features and is plain bagging. Set max_features yourself for regression.
The trade-off works like this:
- A small m lowers correlation between trees but makes each tree weaker, because it sometimes cannot see the best feature. This raises bias.
- A large m makes each tree stronger but more alike. With m = p you get plain bagging.
Building it by hand
The code below fits 25 trees. Each uses a bootstrap sample, and max_features="sqrt" gives each tree a fresh random subset at every split. The trees are combined by majority vote.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0, stratify=y
)
rng = np.random.default_rng(0)
trees = []
for i in range(25):
idx = rng.integers(0, len(X_train), size=len(X_train)) # bootstrap sample
tree = DecisionTreeClassifier(max_features="sqrt", random_state=i)
trees.append(tree.fit(X_train[idx], y_train[idx]))
votes = np.stack([t.predict(X_test) for t in trees])
pred = (votes.mean(axis=0) > 0.5).astype(int) # majority vote for 0/1 labels
print("Hand-built forest accuracy:", (pred == y_test).mean())The dataset has 30 features, so each split considers about 5 of them. Each tree gets a different random_state, so the subsets differ between trees.
Common errors
- Choosing the subset once per tree. The subset must be redrawn at every node. A fixed subset per tree is a different method (random subspaces) and produces weaker trees.
- Leaving regression at the default.
RandomForestRegressorwithmax_features=1.0is bagging, so the trees stay correlated. - Tuning m too low with many noisy features. If few features carry signal, a small m often leaves nodes with only useless candidates. Raise m in that case.
- Reading more trees as a fix. More trees reduce the second variance term only. If accuracy has plateaued, tune
max_features.
Exercise
A dataset has 100 features and one of them is far more predictive than the rest. You train plain bagged trees (all features at every split) and then a forest with max_features="sqrt". Which model has more correlated trees, and why? What does m equal for the forest?
Show answer
The bagged trees are more correlated. The dominant feature wins the root split in nearly every tree, so the trees share their top structure. In the forest, m = √100 = 10, so the dominant feature is absent from the candidate set at about 90% of nodes. Other features must be used there, which makes the trees differ and lowers ρ.
Exercise
In the variance formula ρσ² + (1 − ρ)σ² / B, what is the value as B grows very large, and what does that imply for tuning?
Show answer
The second term goes to zero, leaving ρσ². Adding trees cannot improve on that floor, so to reduce error further you must lower the correlation ρ, for example by reducing max_features.