Lecture 16 · room 202
HEMA · Università Cattolica del Sacro Cuore, Rome
Monday 23 November 2026
Lecture 15 · Correspondence analysis · Tue 17 Nov
| Time | Min | |
|---|---|---|
| 17:00 | Last time · Set 13, the In class questions | 15’ |
| 17:15 | Growing a tree: one split, by hand | 45’ |
| 18:00 | Break | 10’ |
| 18:10 | Too far, and back: pruning | 30’ |
| 18:40 | Reading the tree you printed | 10’ |
| 18:50 | Set 14 | 10’ |
The only model you can print on one page and hand to the credit committee
We are back to supervised learning. The y is back. What changes is that the model is an algorithm, not an equation.
A tree splits the data with yes/no questions on one variable, recursively:
The result partitions the predictor space into rectangles. Each rectangle gets one prediction: the majority class, or the mean of y.
Note
“Purest” is where the whole method lives. For regression it is the residual sum of squares. For classification, two measures — next slide.
Twelve customers, two variables. Press → for each split.
For a node with class proportions p_1, \dots, p_K:
A split is scored by the weighted impurity of its two children. The best split lowers it most. Both measures rank splits almost identically; rpart uses Gini by default.
Ten customers, will they churn? Candidate: tenure < 12 months
Parent node: 5 churn, 5 stay. G = 2 \times 0.5 \times 0.5 = 0.50
| Churn | Stay | G | |
|---|---|---|---|
| tenure < 12 | 4 | 1 | 2 \times 0.8 \times 0.2 = 0.32 |
| tenure ≥ 12 | 1 | 4 | 0.32 |
Weighted child impurity: \tfrac{5}{10} \times 0.32 + \tfrac{5}{10} \times 0.32 = 0.32.
Gain: 0.50 - 0.32 = 0.18. Try balance < 500 and get a gain of 0.02? Then tenure wins, the node splits on it, and the algorithm moves down to each child and starts again.
Left alone, the algorithm splits until every leaf is pure — with n leaves for n points if it has to.
Trees are high-variance: change a few rows, get a different first split, get a different tree.
Grow the full tree T_0. Then for a penalty \alpha \ge 0 find the subtree minimising
\text{RSS}(T) \;+\; \alpha \,|T|
where |T| is the number of leaves. Larger \alpha: smaller tree. Choose \alpha by cross-validation — the same logic as choosing \lambda in LASSO, and for the same reason.
Attention
rpart does this for you, and prints the cross-validation table as cptable. Read it. Pick the cp at the minimum xerror — or the simplest tree within one standard error of it. The default tree it draws is not the pruned one.
library(tidyverse); library(rpart); library(rpart.plot); library(ISLR2)
d <- Default # 10 000 credit-card customers; default yes/no
set.seed(1); tr <- sample(nrow(d), 7000)
fit <- rpart(default ~ balance + income + student, data = d[tr, ], method = "class")
printcp(fit) # the cross-validation table
best <- fit$cptable[which.min(fit$cptable[, "xerror"]), "CP"]
pruned <- prune(fit, cp = best)
rpart.plot(pruned) # the one-page modelThen predict(pruned, d[-tr, ], type = "class") against d$default[-tr] — the confusion matrix — and you have a test error to report.
Default, it is balance < 1800. One variable, and most of the signal.cp was too smallNote
Notice what is not in the tree: income. It was available, and it never split. That is a finding, and it is free.
| For | Against | |
|---|---|---|
| Interpretability | unmatched — the model is the explanation | |
| Interactions | found automatically | |
| Scale, transformations | irrelevant — splits are ordinal | |
| Accuracy | usually beaten by everything on Friday | |
| Stability | change the data a little, get a different tree |
The last two rows are one problem, and Friday’s lecture is one answer: many trees.
cp instead of the minimum. How many leaves do you lose? Does test error move?customers.csv with segment (from week 1) as the target. Which variable is at the top? Is that the variable you would have named the segments by?d[sample(nrow(d), replace = TRUE), ] three times, three trees. Compare the top splits.Decision trees · about 40 minutes · before Fri 27 Nov
Not graded. The test is a mirror, not an exam.
?rpart, ?rpart.plot, ?pruneStatistics & Big Data · HEMA, Università Cattolica del Sacro Cuore