Sub topic 3
Why single trees overfit: bias and variance
This chapter assumes you know supervised learning basics (features, labels, train/test splits), can read short Python and NumPy code, and are familiar with accuracy and mean squared error. It builds on the previous chapters on decision trees and split criteria.
Two sources of error
A model's expected error on new data can be split into three parts:
- Bias is the error from wrong assumptions in the model. A model with high bias is too simple to capture the real pattern. It underfits.
- Variance is how much the fitted model changes when you train it on a different sample of the same size. A model with high variance reacts to noise in the training set. It overfits.
- Irreducible noise is randomness in the labels that no model can predict.
For squared error, the expected test error at a point is bias squared plus variance plus noise. You can only shrink the first two. Lowering one often raises the other. This is the bias-variance trade-off.
Why a fully grown tree has low bias and high variance
By default, a decision tree keeps splitting until every leaf is pure, or until it cannot split further. That has two effects.
- Low bias. A deep tree can carve the feature space into very small regions. It can represent almost any boundary, so it fits the training data perfectly.
- High variance. Each leaf may rest on only a handful of training points. A few noisy or mislabeled points can decide a leaf. The tree also chooses splits greedily, so one different point near the top can change the root split. Everything below it then changes too.
The result is a model with near-100% training accuracy and a clearly lower test accuracy.
Both trees fit their own sample almost perfectly, yet they disagree on new data. That disagreement is variance.
Seeing the gap
The code below trains a fully grown tree and compares training accuracy with test accuracy. It then repeats the fit on several resamples and measures how often the predictions on the test set disagree between fits.
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
)
tree = DecisionTreeClassifier(random_state=0).fit(X_train, y_train)
print("Train accuracy:", tree.score(X_train, y_train))
print("Test accuracy: ", tree.score(X_test, y_test))
rng = np.random.default_rng(0)
preds = []
for _ in range(20):
idx = rng.integers(0, len(X_train), size=len(X_train))
t = DecisionTreeClassifier(random_state=0).fit(X_train[idx], y_train[idx])
preds.append(t.predict(X_test))
preds = np.stack(preds)
# Fraction of test points where the 20 trees do not all agree
unstable = (preds.min(axis=0) != preds.max(axis=0)).mean()
print("Test points with disagreeing trees:", unstable)Training accuracy is 1.0 because the tree memorizes the data. Test accuracy is lower. The last number shows the share of test points whose predicted class changes depending on which resample the tree saw. Exact values depend on your scikit-learn version.
Controlling variance by limiting the tree
You can trade variance for bias by restricting the tree. Common controls are max_depth, min_samples_leaf, and max_leaf_nodes. A shallower tree is more stable, but it may be too coarse and underfit.
| Setting | Bias | Variance | Typical symptom |
|---|---|---|---|
| Very shallow tree | High | Low | Poor on both train and test |
| Moderate depth | Medium | Medium | Train and test close together |
| Fully grown tree | Low | High | Train near perfect, test much lower |
Tuning depth works, but it gives up some of the tree's flexibility. A random forest takes a different route. It keeps each tree deep, so bias stays low, and reduces variance by averaging many trees.
Why averaging helps
Suppose you have B independent models, each with variance σ². The variance of their average is σ²/B. Averaging shrinks variance without changing the expected prediction, so bias stays the same.
Real trees trained on the same data are not independent. If each pair has correlation ρ, the variance of the average is:
ρσ² + (1 − ρ)σ²/B
As B grows, the second term vanishes, but ρσ² remains. To benefit from averaging, the trees must be as different from each other as possible. The next chapters show how bootstrap samples and random feature subsets lower ρ.
Common mistakes
- Judging a tree by training accuracy. A perfect training score says nothing about generalization.
- Assuming a bigger tree is always better. Past a point, extra depth fits noise.
- Assuming that averaging many identical trees helps. If all trees are the same, ρ is 1 and nothing improves.
Exercise
A fully grown tree scores 1.00 on the training set and 0.91 on the test set. You average 200 such trees that were all trained on exactly the same data with the same splits. Will the average beat the single tree? Explain using the variance formula.
Show answer
No. The trees are identical, so the correlation ρ is 1. The variance of the average is ρσ² + (1 − ρ)σ²/B = σ², the same as one tree. Averaging only helps when the trees differ, which is what bootstrap sampling and random feature subsets provide.