Cost complexity pruning
Quick answer
Cost-complexity pruning compares a fitted tree with smaller subtrees by adding a penalty for each leaf. Increasing the penalty favors simpler trees, but the best predictive complexity must be selected using held-out validation data. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Compare two subtree costs
from fractions import Fraction as Ffor a in [F(0),F(1,5),F(1,2)]: print(str(a),str(F(1,10)+2*a),str(F(3,10)+a))Expected output:
0 1/10 3/101/5 1/2 1/21/2 11/10 4/5At 1/5 the costs tie. Larger alpha favors one leaf.
How it works
Let R(T) be the sum of leaf impurities weighted by the fraction of training weight in each leaf. The objective is R_alpha(T)=R(T)+alpha*L(T), where L(T) is the number of leaves. Suppose a two-leaf tree has R=0.10 and replacing it with a single leaf gives R=0.30. The two objectives coincide at alpha=0.20. Below that value the two-leaf tree has smaller penalized risk; above it the single leaf wins. For a branch with L leaves, the effective alpha is (R(collapsed leaf)−R(branch))/(L−1). Weakest-link pruning removes branches with the smallest effective alpha to form a sequence of nested subtrees. The scikit-learn cost_complexity_pruning_path returns candidate ccp_alphas and total leaf impurities; compute the path using training data only. Larger ccp_alpha tends to produce fewer leaves along this path; it does not guarantee better accuracy. In cross-validation, derive candidates inside training folds or use a prespecified grid. Choose alpha using validation performance, tie-breaking toward simpler trees if declared beforehand; do not retune on the test set. Use export_text or decision_path to understand learned rules. An impurity importance value describes the fitted tree, not an intervention. Permutation importance measures performance changes on a chosen dataset and can understate interchangeable correlated features.
Inspect a pruning path
from sklearn.tree import DecisionTreeClassifierX=[[0],[1],[2],[3]]; y=[0,0,1,1]m=DecisionTreeClassifier(random_state=7)p=m.cost_complexity_pruning_path(X,y)print(p.ccp_alphas.tolist(),p.impurities.tolist())print([int(DecisionTreeClassifier(ccp_alpha=float(a),random_state=7).fit(X,y).get_n_leaves()) for a in p.ccp_alphas])Expected output:
[0.0, 0.5] [0.0, 0.5][2, 1]The final path point collapses the stump to the root. This path uses only training data.
Common mistakes
Do not confuse the pruning penalty with a probability cutoff. Do not compute pruning paths on combined train and test labels. Do not interpret a feature importance ranking as causality.
Practice and next steps
Compare a four-leaf tree with risk 0.05 and a two-leaf subtree with risk 0.15. Find their tie alpha, then plan a validation-only comparison. Complete the knowledge check and coding exercise. Next: Bootstrap sampling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cost complexity pruning — knowledge check
Apply cost complexity pruning to new examples.
What does alpha penalize?
Two leaves have risk 0.1; one leaf risk 0.3. Tie alpha?
Which split should produce pruning candidates?
What does a larger alpha usually do along a pruning path?
Does large impurity importance establish causation?