Cost-Complexity Pruning
A decision tree can keep splitting until it perfectly fits the training data—like memorizing every example. The problem is that “perfect memory” usually means worse predictions on new data. Cost-complexity pruning is a principled way to trim a tree back to the size that generalizes well.
What it is and what it optimizes
Cost-complexity pruning (also called weakest-link pruning) chooses a subtree by balancing two things: how well the tree fits the training data and how complicated the tree is. It does this with an objective like:
R_α(T) = R(T) + α · |T|
Here R(T) is the training error (for classification, impurity-based error; for regression, squared error), |T| is the number of leaves (a proxy for complexity), and α controls how much you penalize complexity. Larger α pushes toward smaller trees.
How the pruning actually happens
You start with a fully grown tree, then repeatedly remove the “weakest link”: the internal node whose removal increases training error the least per leaf removed. This generates a sequence of nested subtrees from large to small. You then pick the best subtree by evaluating candidates—typically with cross-validation—over different α values.
Why it matters in real supervised tasks
- Spam detection: prevents rules that key off rare, quirky words seen only in training.
- Credit scoring: avoids overly specific splits that look great historically but fail on new applicants.
- House price prediction: reduces sensitivity to noise (e.g., one odd sale driving a deep branch).
In scikit-learn, this is exposed via minimal cost-complexity pruning using ccp_alpha in DecisionTreeClassifier/DecisionTreeRegressor, and you can inspect candidate alphas with cost_complexity_pruning_path.
Cost-Complexity Pruning is a decision-tree pruning method that selects a subtree by minimizing a penalized objective: training error (or impurity) plus a penalty proportional to the number of terminal nodes, controlled by a complexity parameter. It produces a sequence of nested subtrees and chooses the best trade-off, typically via cross-validation. It matters because it reduces overfitting and yields smaller, more generalizable trees without sacrificing predictive accuracy.
Imagine trimming a real tree in your yard. If you let it grow wild, it gets messy and fragile. If you cut too much, it stops being useful for shade. Cost-Complexity Pruning is that same idea for a decision tree in AI.
A decision tree can keep adding little branches to explain every tiny detail in the training data. That often makes it “memorize” quirks and do worse on new cases. Cost-Complexity Pruning deliberately cuts back branches, balancing two goals: being accurate on past examples (the “cost”) and staying simple (the “complexity”). The result is usually a sturdier model that generalizes better.