Sub topic 6
When to stop growing
This chapter assumes you know how a tree is built by recursive partitioning, how Gini impurity scores a node, and how to run scikit-learn's DecisionTreeClassifier.
Why a stopping rule is needed
Recursive partitioning keeps splitting until something tells it to stop. With no rule, a tree on noisy data splits until every leaf holds one sample or one class. Training accuracy then reaches 100%, but the tree has memorized noise. A stopping criterion is a test applied at each node before it is split. If the test fires, the node becomes a leaf.
The common criteria
These are the standard scikit-learn parameters. Each one stops growth in a different way.
| Parameter | What it limits | Default |
|---|---|---|
| `max_depth` | Number of splits from root to any leaf | `None` |
| `min_samples_split` | Smallest node that may still be split | 2 |
| `min_samples_leaf` | Smallest allowed leaf | 1 |
| `min_impurity_decrease` | Smallest weighted impurity decrease a split must achieve | 0.0 |
| `max_leaf_nodes` | Total leaves, grown best-first | `None` |
Depth and leaf-count limits cap the overall size. Sample-based limits keep leaves from resting on a handful of points, which makes their predicted class proportions unreliable. min_impurity_decrease rejects splits that barely help.
Comparing settings
The script below uses noisy synthetic data, since a tree on clean data has little to overfit. It prints the leaf count and train and test accuracy for each setting.
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_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
settings = {
"no limits": {},
"max_depth=4": {"max_depth": 4},
"min_samples_leaf=10": {"min_samples_leaf": 10},
"min_impurity_decrease=0.005": {"min_impurity_decrease": 0.005},
"max_leaf_nodes=12": {"max_leaf_nodes": 12},
}
for name, params in settings.items():
clf = DecisionTreeClassifier(random_state=0, **params).fit(X_train, y_train)
print(
f"{name:30s} leaves={clf.get_n_leaves():3d} "
f"train={clf.score(X_train, y_train):.3f} "
f"test={clf.score(X_test, y_test):.3f}"
)Expect the unrestricted tree to show train accuracy near 1.0 with the most leaves and a lower test score. The limited trees should lose some training accuracy and close the gap between train and test. Exact numbers depend on your scikit-learn version.
Common errors
- Confusing
min_samples_splitwithmin_samples_leaf. The first only controls whether a node may be split. A split of a 20-sample node can still produce a 1-sample child. The second guarantees every leaf is large enough, so it is the stronger smoothing control. - Choosing values by test score. Looking at the test column above is fine for illustration. If you pick the setting with the best test score, the test set is no longer an honest estimate. Select values with cross-validation, covered in the evaluation chapter.
- Setting limits too tight. A very small
max_depthunderfits. The tree cannot express interactions, so both train and test scores are low.
A limit of greedy stopping
A split is judged one step at a time. On XOR-like data, no single split reduces impurity, so a strict min_impurity_decrease can stop the tree at the root even though two splits together would separate the classes. Stopping early is cheap but short-sighted. The next chapters cover pruning, which grows a large tree first and then cuts it back.
Exercise
A tree uses minsamplessplit=20. Can it still produce a leaf with one sample? How would you rule that out?
Show answer
Yes. A node with 25 samples may be split, and the best threshold may send 24 samples one way and 1 the other. Set minsamplesleaf (for example to 5 or 10), which rejects any split that would leave a child smaller than that value.