Sub topic 7
Why deep trees overfit
This chapter assumes you can train a scikit-learn classifier, know the difference between training and test sets, and have seen how a tree picks splits by impurity. Here you will see why an unrestricted tree memorizes its training data and how to spot it.
What overfitting means for a tree
A model overfits when it fits noise in the training set instead of the pattern that generalizes. A tree is especially prone to this. If you let it grow until every leaf is pure, it can separate any training set in which no two samples share the same features but have different labels. Training accuracy then reaches 100%, whatever the data is.
Each split divides the data. Deeper nodes hold fewer samples, so their splits rest on less evidence. A leaf holding one sample predicts that sample's label, even if the label was a mislabeled point or a random fluke.
Variance: the root cause
Overfit trees have high variance. A small change in the training data, such as removing a few rows, can change an early split. Every split below it then changes too, so the whole tree looks different. The model follows the sample instead of the underlying distribution.
Seeing the gap
The check is to compare training and validation scores as complexity grows. This script trains trees of increasing depth on noisy data and prints both scores.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = make_classification(
n_samples=600, n_features=10, n_informative=4,
flip_y=0.1, random_state=0,
)
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
for depth in [1, 2, 3, 5, 8, 12, None]:
clf = DecisionTreeClassifier(max_depth=depth, random_state=0)
clf.fit(X_train, y_train)
print(
f"depth={str(depth):>4} leaves={clf.get_n_leaves():>3} "
f"train={clf.score(X_train, y_train):.3f} "
f"val={clf.score(X_val, y_val):.3f}"
)The flip_y=0.1 setting randomly flips the labels of about 10% of samples, which acts as label noise. With max_depth=None, training accuracy reaches 1.000 while validation accuracy stays well below it. The tree has memorized the flipped labels. Exact numbers depend on your scikit-learn version, but the pattern holds.
Reading the symptoms
| Symptom | What it indicates |
|---|---|
| Train and validation both low | Underfitting: the tree is too shallow |
| Train near 100%, validation much lower | Overfitting: the tree is too complex |
| Many leaves with 1 or 2 samples | Splits supported by almost no data |
| Validation score changes a lot between random seeds or folds | High variance |
Common errors
- Judging by training accuracy. A pure-leaf tree always scores near perfect on its own training data, so this number tells you nothing about generalization.
- Tuning on the test set. If you choose depth by looking at test accuracy, the test score becomes optimistic. Use cross-validation on the training data, covered in a later chapter.
- Blaming too little data only. More data helps, but a tree that grows until leaves are pure will still chase noise. Complexity limits matter as well.
- Assuming one good split means a good tree. Greedy splitting commits to early choices and never revisits them, which adds to instability.
What to do next
Limiting depth, requiring a minimum number of samples per leaf, and pruning all reduce variance by keeping the tree small. The next chapters cover how to evaluate trees honestly and how to prune them.
Exercise
A decision tree with no depth limit scores 100% on the training set and 78% on the validation set. A tree with max_depth=4 scores 85% on training and 81% on validation. Which tree is likely to generalize better, and why?
Show answer
The depth-4 tree. Its validation score is higher (81% vs 78%), and its small gap between training and validation shows it is not memorizing noise. The unrestricted tree has pure leaves that fit the training labels exactly, including noise, which is overfitting caused by high variance.