Why Prune a Tree That Fits the Training Data?
A tree can reduce its training error by adding splits: each split gives the model more regions in which to make separate predictions. But a very large tree may capture chance patterns in the training sample, produce complicated rules, and predict poorly on new cases. Pruning addresses this trade-off by starting with a fitted tree and considering simpler trees formed by removing branches.
The cost-complexity objective makes the trade-off explicit. It adds a penalty for tree size to the training loss. A larger penalty favors smaller trees; a smaller penalty gives more weight to fitting the training data. The objective can be used with regression or classification, provided the training loss is defined consistently for every candidate tree.
For a regression tree, a common choice for \(R(T)\) is the residual sum of squares (RSS) on the training data. For a classification tree, one possible choice is the number of training cases misclassified by the tree. Other loss definitions can be used, but their scale matters: changing the loss scale without changing \(\alpha\) changes the balance between fit and size.
The penalty is charged per terminal node, not per split or per level of depth. A tree with five terminal nodes therefore receives penalty \(5\alpha\), regardless of whether its branches are balanced. If an implementation reports an objective using a scaled loss or a different size convention, its numerical penalty parameter need not be directly comparable to an \(\alpha\) defined here.
What It Means to Prune a Branch
A branch, or subtree, consists of an internal node and all of its descendants. Pruning that branch removes its descendant splits and replaces the entire branch with one terminal node at its root. The rest of the tree is unchanged. In particular, pruning a branch with several leaves does not remove the leaves elsewhere in the tree.
If the branch being considered has \(L\) terminal nodes before pruning, collapsing it changes the total leaf count by \(L-1\): those \(L\) leaves are replaced by one. When the leaf predictions are refitted according to the chosen tree-fitting rule, merging regions cannot improve the optimal training loss relative to keeping them separate. Thus, the training loss usually increases or stays the same as a result of pruning.
Why the Pruning Rule Holds
The rule follows by comparing the objective before and after a single branch is collapsed. Let the tree outside the branch have training loss \(R_{\text{outside}}\) and \(K\) terminal nodes. Before pruning, the branch contributes loss \(R_{\text{sub}}\) and \(L\) leaves. After pruning, it contributes loss \(R_{\text{leaf}}\) and one leaf. Any loss and leaves outside the branch appear in both candidate objectives.
Subtracting the objective before pruning from the objective after pruning cancels the contributions outside the branch:
The pruned version is at least as good when this difference is nonpositive. Rearranging gives \(\Delta R\leq\alpha(L-1)\), as stated. This derivation also explains the two competing effects: pruning pays an increase in training loss, but saves the penalty on \(L-1\) leaves.
Worked Example: Comparing Two Branches in a Regression Tree
A hypothetical fitted regression tree has two branches, A and B, and four terminal nodes in total: two leaves in each branch. Its training RSS is 24. Collapsing A increases RSS by 6; collapsing B increases RSS by 5. Compare the full tree, each singly pruned version, and the tree with both branches pruned when \(\alpha=4\). Use the cost-complexity objective with RSS as the training loss.
Method. For each candidate, add its RSS to \(4\) times its number of terminal nodes. The full tree has four leaves, so its objective is \(24+4(4)=24+16=40\).
If A alone is collapsed, RSS becomes \(24+6=30\). The two leaves in A become one, while B’s two leaves remain, so there are three leaves. The objective is \(30+4(3)=30+12=42\).
If B alone is collapsed, RSS becomes \(24+5=29\). A still has two leaves and B now has one, giving three leaves. The objective is \(29+4(3)=29+12=41\).
If both branches are collapsed, the RSS increases by both stated amounts: \(24+6+5=35\). Each branch becomes one terminal node, so there are two leaves in total, not one. The objective is \(35+4(2)=35+8=43\).
The full tree has the smallest objective, \(40\), among these four candidates. At \(\alpha=4\), neither branch should be collapsed when comparing these candidates. The branch rule gives the same check: for A, the loss increase is \(6\), while the saved penalty is \(4(2-1)=4\); for B, the increase is \(5\), also greater than the saved penalty of \(4\). Common mistake avoided: collapsing two separate branches leaves two terminal nodes, one for each branch. It does not leave one terminal node for the whole tree.
Regression and Classification Use the Same Structure
The cost-complexity comparison has the same form for either task. What changes is the definition of training loss. With regression RSS, a loss increase is measured in squared outcome units. With classification misclassification loss, it is measured in cases (or, if divided by the sample size, in a proportion). The value of \(\alpha\) must be interpreted on the same scale as the chosen loss.
| Tree type | Example of training loss \(R(T)\) | What pruning trades off |
|---|---|---|
| Regression | Sum of squared differences between observed and predicted responses | Increase in squared error versus fewer terminal nodes |
| Classification | Number of training cases assigned to the wrong class | Increase in misclassified cases versus fewer terminal nodes |
Worked Example: Pruning a Classification Subtree
A hypothetical classification tree contains a subtree with three terminal nodes. The subtree misclassifies 12 training cases. If it is collapsed to one terminal node, it misclassifies 19 cases. The tree outside this subtree is unchanged. Using the number of misclassified cases as \(R(T)\), determine whether to collapse the subtree when \(\alpha=4\).
Method. Compare the objective change for collapsing with zero. The training-loss increase is \(\Delta R=19-12=7\). The leaf count falls from three to one, so the penalty saving is \(4(3-1)=4(2)=8\).
The objective change is increase in loss minus saved penalty: \(7-8=-1\). A negative change means the collapsed tree has the smaller objective. Direct calculation confirms this. Before pruning, the subtree contributes \(12+4(3)=12+12=24\); after pruning, it contributes \(19+4(1)=19+4=23\).
Therefore, collapse the subtree under this objective: its one-case-equivalent increase in training loss is more than compensated for by the penalty saving. Common mistake avoided: comparing the loss increase of 7 with \(\alpha\) alone would ignore that pruning removes two leaves. The correct penalty saving is \(\alpha(L-1)=8\).
Worked Example: Selecting Among Nested Regression Trees
A hypothetical regression tree and two successively pruned versions have the following training RSS values and leaf counts: the largest tree has RSS 18 and 6 leaves; pruning one branch gives RSS 22 and 4 leaves; pruning further gives RSS 29 and 2 leaves. For \(\alpha=2.5\), determine which of these candidates has the smallest cost-complexity objective.
Method. Calculate \(R(T)+\alpha|T|\) for each candidate, retaining the stated leaf count for each entire tree. For the largest tree, the objective is \(18+2.5(6)=18+15=33\).
For the tree with four leaves, the objective is \(22+2.5(4)=22+10=32\).
For the tree with two leaves, the objective is \(29+2.5(2)=29+5=34\).
The four-leaf tree has the smallest objective, \(32\), so it is preferred among these candidates. The largest tree has lower training RSS, but the reduction in RSS does not offset its additional leaf penalty. The two-leaf tree is smaller, but its increase in RSS is too large relative to the penalty saved. Common mistake avoided: choosing the tree with the lowest training loss alone ignores the complexity term; choosing the smallest tree alone ignores the fit term.
Practical Interpretation and Exam Traps
Cost-complexity pruning is a post-growth simplification: it considers branches in a tree that has already been fitted, rather than preventing a split from being created in the first place. This distinguishes pruning from pre-pruning rules such as stopping growth at a specified depth or minimum node size. The earlier material on tree-growth stopping rules addresses those restrictions; the criterion here evaluates the fit-size trade-off for candidate subtrees.
The chosen loss needs to be stated before making a numerical comparison. For example, an RSS value and a misclassification count cannot be combined in one objective without a defined conversion. Likewise, if a classification loss is recorded as a proportion rather than a count, the penalty must be on a compatible scale. The objective is a comparison tool, not a claim that every unit of training loss has the same practical meaning across problems.
The main arithmetic traps are straightforward but consequential:
- Counting leaves rather than removed splits. The penalty is based on terminal nodes. Collapsing a subtree with \(L\) leaves saves \(\alpha(L-1)\), not simply \(\alpha\) unless \(L=2\).
- Counting the resulting leaves incorrectly. A collapsed branch contributes one leaf, but leaves outside it remain. If two separate branches are collapsed, each contributes one leaf.
- Reversing the comparison. The pruned objective minus the original objective is \(\Delta R-\alpha(L-1)\). A negative value favors pruning; a positive value favors retaining the branch.
- Ignoring ties. If the difference is zero, both candidates have equal cost-complexity objective. The criterion alone does not select uniquely between them.
- Mixing loss scales. A penalty value has meaning only in relation to the chosen definition of \(R(T)\) and its scale.
The penalty term explains why a larger tree is not automatically better, even when it fits the training data more closely. A tree’s training loss generally rewards additional flexibility, while the terminal-node penalty charges for it. Pruning is worthwhile when the reduction in complexity penalty is at least as large as the resulting increase in training loss.
Check Your Understanding
For each problem, use the stated loss and compare cost-complexity objectives. Treat a tie as equal preference under the criterion alone.
- A regression subtree has three leaves and RSS 14. Collapsing it gives RSS 20. If \(\alpha=2.5\), should it be collapsed? (Answer: Keep the subtree: its objective contribution is \(14+2.5(3)=21.5\), versus \(20+2.5(1)=22.5\) after collapsing.)
- A classification subtree has four leaves and misclassifies 9 training cases. A single-leaf version misclassifies 18 cases. If \(\alpha=3\), compare the two contributions. (Answer: Tie: \(9+3(4)=21\) and \(18+3(1)=21\).)
- A regression tree has five leaves and RSS 26. Pruning one branch replaces three leaves in that branch with one, giving RSS 31 and three leaves overall. If \(\alpha=2\), should the branch be pruned? (Answer: Keep it: the full tree has objective \(26+2(5)=36\), versus \(31+2(3)=37\) after pruning.)
- Three nested regression-tree candidates have (RSS, leaf count) values \((20,6)\), \((24,4)\), and \((30,2)\). Which has the lowest objective at \(\alpha=2.5\)? (Answer: The four-leaf candidate: the objectives are \(20+2.5(6)=35\), \(24+2.5(4)=34\), and \(30+2.5(2)=35\).)
- A classification tree has seven leaves and misclassifies 16 cases. Collapsing a subtree that contains four leaves increases the error by 5 cases and leaves four terminal nodes in the whole tree. At \(\alpha=1.5\), should that subtree be collapsed? (Answer: Keep it: the full objective is \(16+1.5(7)=26.5\); after pruning it is \(21+1.5(4)=27\).)