liveBook · Manning

Pruning

As explained in Statistics Every Programmer Needs

Pruning is the removal of branches or subtrees from a decision tree to simplify the model and limit unnecessary complexity.

Pruning limits a decision tree’s complexity by removing branches or preventing them from growing. Its purpose is to reduce overfitting: splits that add little predictive value can make a tree harder to interpret without helping it generalize to unseen data. section 6.3.2

One practical approach is to constrain growth during fitting rather than trim a completed tree. Setting max_depth=3 limits how deep the classifier can split. This is a depth limit, not a separate statistical-significance test of each branch; the split criterion, such as Gini impurity or entropy, serves a different purpose. section 6.3.2

>>> from sklearn.tree import DecisionTreeClassifier
>>> clf = DecisionTreeClassifier(criterion = 'gini', \
>>>                              max_depth = 3, random_state = 0)

This scikit-learn configuration caps tree depth at three, chooses Gini impurity to evaluate splits, and fixes the random seed for reproducibility. Fit the configured classifier on X_train and y_train, then make predictions on the unseen test subset. section 6.3.2

A smaller tree is easier to trace from feature conditions to a predicted class, but simplicity alone does not establish predictive accuracy. Inspect the plotted splits and assess predictions on data withheld from fitting. Pruning removes splits and features, not training records: observations remain assigned to nodes in the resulting tree. section 6.3.2 section 6.3.6

Fourth-down conversion tree

A depth-limited classifier predicts whether a fourth-down attempt converts. Its plotted nodes show conditions such as TO_GO <= 3.5, sample counts, class counts, and predicted classes. The resulting tree has fewer splits and features than an unrestricted version would have, making its decision paths more manageable to inspect. section 6.3.5 section 6.3.6

Pruning addresses one weakness of decision trees, but it is not a complete remedy: trees can still be unstable when data changes or perform poorly on imbalanced data without appropriate tuning. A random forest is another option for mitigating some single-tree disadvantages. section 6.3.7

How Statistics Every Programmer Needs covers it

How deep it goes
Defines pruning, demonstrates a maximum-depth constraint, and interprets its visual result without detailing a separate post-fitting pruning algorithm.
Examples
Uses Python classifier configuration and a plotted fourth-down conversion tree with feature thresholds, sample counts, and class predictions.
What it assumes
Builds on training/test splits, feature-based classification, and fitting a classifier while briefly explaining split impurity.
What you can do after
Apply it via scikit-learn’s DecisionTreeClassifier(max_depth=3) and inspect the fitted tree with plot_tree().
How it connects
Connects decision trees, overfitting, generalization, split impurity, and random forests.

Also explained in

Compare how 7 books explain pruning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.