← 返回学院
An instructor steadies a branching wooden model as a learner removes a joined pair of discs from its lower branch.
AI 知识核心2026年11月14日· 2 分钟

Cost-Complexity Pruning for Decision Trees

Cost-complexity pruning grows a full decision tree, then compares nested smaller trees by adding a penalty per leaf. Validation picks the penalty, and a reserved test set gives the final honest score.

cost-complexity pruningdecision treesvalidation

来源

完整课程摘要

At the bottom of a decision tree, two leaves send nearly identical predictions to their respective cases. Would the tree work as well if their parent made a single prediction? Cost-complexity pruning compares the fit of a subtree with the cost of retaining its leaves. The score combines a training cost with a penalty for each leaf. Its tuning parameter, alpha, sets how much a new leaf adds to that bill. Larger values make simpler subtrees more attractive. In scikit-learn the training cost is based on the sample-weighted impurity of the leaves.

Suppose a four-leaf tree for classifying fictional reading responses has total weighted impurity of 0.20. A six-leaf alternative reduces that to 0.18. At alpha equal to 0.02, the four-leaf cost is 0.20 plus four times 0.02, or 0.28. The six-leaf cost is 0.18 plus six times 0.02, or 0.30. The smaller tree wins. Now lower alpha to 0.005. The scores become 0.22 and 0.21. The larger tree wins because its small improvement in fit now covers the lower price of two extra leaves.

A common misconception is that the weakest branch is the one with the fewest samples. In fact, the effective α depends on both the impurity reduction and the number of leaves removed, so a branch with many samples can still go first. Also, pruning order comes from the training data. To choose α, compare candidate subtrees on a validation set. Keep a final test set untouched until one tree and one α are selected. Otherwise the test score becomes another tuning signal and overstates performance.

The costs on that worksheet are meaningful only when the two trees use the same sample, weights and impurity definition. A teacher can ask what happens at alpha equal to 0.01. The smaller tree scores 0.24 and the larger tree scores the same. That is the price at which the gain from the two extra leaves just pays for their retention. It still leaves open a separate question about which tree predicts fresh responses more accurately.

In class, give pairs a printed six-leaf toy tree with one marginal split. Ask them to add the weighted leaf impurity and the leaf penalty for each tree at α = 0, 0.01 and 0.05, then mark which tree wins at each value. Pairs write a one-sentence rule for when the marginal split survives. Post the rules and compare them. The visible reasoning shows whether students track both fit and penalty, or only count leaves.

This method offers a transparent trade-off, not a guarantee of good generalization. Its value lies in making the cost of complexity explicit and in keeping selection and final evaluation separate. A tree chosen this way still needs a fresh test set to estimate how it will behave on new students.