CS5100 Model Map ⌂ Modules
Stage 13 of 18Week 4 · Trees and neural networksabout 35 min

Regression trees

Ask a sequence of yes/no questions, each about one input. The answers cut the input space into boxes, and each box predicts the average of its training points. Growing the tree is a greedy search for the cut that lowers the RSS most.

Decision trees were on both the 2024 and 2025 papers (25 marks each time), and the lecturer’s week 4 matches 2025 Q1 almost exactly. Stages 13 to 15 take you through every part.

Book: what to read for this stage

ISLP 8.1 intro and 8.1.1 Regression trees up to “Tree pruning” (Hitters example, eqs. 8.1 to 8.3): read. 8.1.3 Trees versus linear models and 8.1.4 advantages and disadvantages: read (short, and lab Q1 is that comparison). 8.2 (bagging, random forests, boosting): skip for now; it is the most-examined second-half topic, so it will get its own stage when it’s taught.

1. Boxes from yes/no questions

A tree splits the input space with questions “is \(X_j < s\)?”. Each answer sends you left (yes) or right (no) until you reach a leaf:

             Years < 4.5 ?
            /             \
          yes              no
       R1: 5.11      Hits < 117.5 ?
                     /           \
                   yes            no
                R2: 6.00       R3: 6.74

This is ISLP Fig. 8.1: a baseball player’s log salary from Years and Hits. The leaf value is the average of the training players in it. The regions are boxes with edges parallel to the axes (\(R_1 = \{\text{Years} < 4.5\}\), \(R_2 = \{\text{Years} \ge 4.5, \text{Hits} < 117.5\}\), \(R_3 = \{\text{Years} \ge 4.5, \text{Hits} \ge 117.5\}\)); they don’t overlap and they cover everything.

Vocabulary: the root is the first question; internal nodes are questions; leaves (terminal nodes) are the regions; the depth is the number of questions on the longest path.

Predicting: follow the questions to a leaf \(R_x\) and output the average \(\hat y = \frac{1}{|R_x|}\sum_{i \in R_x}y_i\) of its training responses. As a formula, \(f(X) = \sum_m c_m\mathbb 1(X \in R_m)\): piecewise constant.

Why people like trees: you can read them like a flow chart (Years is the most important input; for inexperienced players Hits barely matters), they handle categorical inputs easily, and they need no scaling (rescaling an input just rescales its thresholds). Weakness: high variance; a small change in the data can change the whole tree.

Using the tree above, what log-salary is predicted for a player with 10 years and 100 hits?

Years \(\ge 4.5\), so go right; Hits \(< 117.5\), so go left: leaf \(R_2\), 6.00. (A player with 3 years gets 5.11 whatever their hits.)

2. Growing a tree: greedy splitting by RSS

Score a tree \(T\) with leaves \(R_1, \dots, R_J\) by its training RSS:

\[\mathrm{RSS}(T) = \sum_{m=1}^{J}\sum_{i \in R_m}(y_i - \hat y_{R_m})^2.\]

(The leaf average is the best constant: \(\frac{d}{dc}\sum(y_i - c)^2 = 0\) gives \(c = \bar y\).)

Trying every possible tree is impossible, so recursive binary splitting works top-down and greedily:

  1. Start with one region holding all the data.
  2. Try every input \(j\) and every cutpoint \(s\), splitting into \(\{x_j < s\}\) and \(\{x_j \ge s\}\). Keep the split with the smallest RSS.
  3. Then try every split of any current region; keep the one with the smallest total RSS (= the biggest drop). Repeat.

Only the gaps between neighbouring data values matter, so the candidate cutpoints are the midpoints of neighbouring distinct values. Greedy means a split is never revisited, even if a different early split would have allowed a better tree later.

Worked example. \(x = (1, 2, 3, 5, 7, 8)\), \(y = (2, 3, 5, 8, 9, 12)\). Root mean 6.5, root RSS 73.5.

cut \(s\) left \(y\), mean, RSS right \(y\), mean, RSS total
1.5 {2}, 2, 0 {3, 5, 8, 9, 12}, 7.4, 49.2 49.2
2.5 {2, 3}, 2.5, 0.5 {5, 8, 9, 12}, 8.5, 25 25.5
4 {2, 3, 5}, 3.33, 4.67 {8, 9, 12}, 9.67, 8.67 13.33
6 {2, 3, 5, 8}, 4.5, 21 {9, 12}, 10.5, 4.5 25.5
7.5 {2, 3, 5, 8, 9}, 5.4, 37.2 {12}, 12, 0 37.2

Best first split: \(s = 4\). Second split: the left region’s best cut (at 2.5) saves \(4.67 - 0.5 = 4.17\); the right region’s best cut (at 7.5) saves \(8.67 - 0.5 = 8.17\). The right one wins: leaves \(x < 4 \to 3.33\), \(4 \le x < 7.5 \to 8.5\), \(x \ge 7.5 \to 12\), total RSS 5.17.

3. When to stop, and why it matters

Keep splitting until a stopping rule: a maximum depth (max_depth) or a minimum number of points per leaf (\(n_{\text{stop}}\)). With \(n_{\text{stop}} = 1\) every leaf holds one training point, predicts it exactly, and the training RSS is 0: the tree has memorised the data, noise included. That’s extreme overfitting. More leaves = more flexible (stage 15 controls this with pruning).

Trees vs linear models (ISLP 8.1.3): if the truth is close to a straight line, a tree must approximate it with a staircase and linear regression wins. If the truth is very non-linear or box-shaped, the tree wins.

In sklearn: DecisionTreeRegressor(max_depth=10).fit(X, y), and plot_tree shows each node’s split (sklearn sends “\(\le\)” left), its squared_error (MSE = RSS/n), samples, and value (the mean).

Why does rescaling income from pounds to thousands of pounds change KNN’s predictions but not a tree’s?

Splits depend only on the order of the values within one input, never on distances across inputs.

Stage test

(4 marks, exam formats 1 and 2.) Explain what a regression tree is, how it predicts, and write the quantity minimised when choosing a split.

A regression tree divides the input space into non-overlapping rectangular regions (boxes) using a sequence of yes/no questions of the form “is \(X_j < s\)?”; each question is an internal node and the final regions are the leaves. To predict for a new \(x\) we follow the questions to its leaf and output the average response of the training points in that leaf. The tree is grown top-down and greedily: at each step, over every region, input \(j\) and cutpoint \(s\), we choose the split that minimises \(\sum_{i: x_{ij} < s}(y_i - \bar y_{low})^2 + \sum_{i: x_{ij} \ge s}(y_i - \bar y_{high})^2\) (the RSS of the two new regions), and we stop when the leaves reach a minimum size or the tree a maximum depth.

A leaf holds responses 4, 6 and 11. What is its RSS?

Mean 7. RSS \(= 9 + 1 + 16 = 26\). The leaf predicts 7.

A region has RSS 50. Split A gives halves with RSS 20 and 15; split B gives 5 and 32. By how much does the chosen split reduce the RSS?

A totals 35, B totals 37. Greedy picks A: a drop of \(50 - 35 = 15\).

A tree is grown until every leaf contains a single training point. What is its training RSS, and is that good?

Each leaf predicts its own point exactly. Training error says nothing about test error.

The true relationship between \(X\) and \(Y\) is a straight line plus noise. Which model is likely to do better on test data?

A tree can only build a staircase approximation of a line (ISLP 8.1.3).