Unit 7, lesson 49

Regression Trees and Pruning

How a regression tree predicts leaf means, how a squared-error split is chosen, how tree depth and minimum leaf size limit flexibility, and how cost-complexity pruning and validation choose the size of a tree.

  • Video 49
  • Decision Trees

A shop wants to predict daily demand from temperature, and in its records demand jumps once the weather turns warm instead of rising along a straight line. A regression tree can follow such a jump without being told where it is, by cutting the temperature axis into intervals and predicting a constant on each one. Where should the cuts go, and how many should there be? This lesson fits the constants, scores cuts by squared error, and controls the size of the tree with depth limits, minimum leaf sizes and pruning, with validation data rather than training error making the final choice.

Key terms

Regression tree

Definition A regression tree predicts a numerical target yy from a feature vector x\mathbf{x}. Like a classification tree, it is a rooted binary tree whose internal nodes hold tests such as xj≤tx_j \le t, which send an input to the left child when the test holds and to the right child otherwise, and whose leaves define disjoint regions R1,…,RMR_1, \dots, R_M that together cover feature space. Leaf mm holds a constant cmc_m, and the prediction is

f^(x)=cmfor x∈Rm.\hat{f}(\mathbf{x}) = c_m \quad \text{for } \mathbf{x} \in R_m .

When the tree is fitted with squared error, each constant cmc_m is the mean of the training targets in RmR_m. With one numerical feature the fitted function is a step function, constant on intervals and jumping only at the thresholds. Every prediction is an average of training targets, so it lies between the smallest and the largest training target. An input beyond the range of the training inputs simply receives the constant of the leaf it falls into, so a regression tree never continues a trend beyond the data.

In plain words A regression tree sorts each input into a bin by asking yes or no questions and predicts the average target of the training rows in that bin. It can follow a jump or a curve without a formula for it, it can also follow noise, and it predicts a flat value beyond the edges of the data.

Example The constructed data on this page pair a normalized temperature xx with a demand yy in units sold.

Temperature xx112233445566
Demand yy223344101011111212

The tree with the single test x≤3.5x \le 3.5 predicts 33 when x≤3.5x \le 3.5 and 1111 when x>3.5x > 3.5. A temperature of exactly 3.53.5 goes left and gets 33, while 3.50013.5001 gets 1111. Outside the data, the temperature 00 gets 33 and the temperature 77 gets 1111, whereas a fitted straight line would keep rising. Whether a flat continuation is sensible depends on the application, and these six rows cannot settle it.

Leaf mean

Definition Let a leaf hold m≥1m \ge 1 training targets y1,…,ymy_1, \dots, y_m with mean yˉ\bar{y}. For every constant cc,

∑i=1m(yi−c)2=∑i=1m(yi−yˉ)2+m(yˉ−c)2,\begin{aligned} \sum_{i=1}^{m} (y_i - c)^2 &= \sum_{i=1}^{m} (y_i - \bar{y})^2 \\[0.4em] &\quad + m(\bar{y} - c)^2 , \end{aligned}

because the cross term 2(yˉ−c)∑i(yi−yˉ)2(\bar{y} - c)\sum_{i} (y_i - \bar{y}) is zero. The last term is positive unless c=yˉc = \bar{y}, so the leaf mean is the unique constant with the smallest sum of squared errors in the leaf, and that smallest sum, ∑i(yi−yˉ)2\sum_{i} (y_i - \bar{y})^2, is the leaf’s sum of squared errors, or SSE. An empty leaf has no mean, which is one reason a split must leave both children nonempty. The mean is the answer for squared error in particular. Under absolute error any median of the leaf’s targets is a best constant, and with row weights the best constant under squared error is the weighted mean.

In plain words Predicting the leaf mean is not a convention. Under squared error every other constant does worse, by an amount that grows with the square of its distance from the mean.

Example The left leaf of the split at 3.53.5 holds the targets 2,3,42, 3, 4. Its mean is 33, its residuals are −1,0,1-1, 0, 1, and its SSE is 22. The constant 44 gives squared errors 4,1,04, 1, 0, a total of 5=2+3⋅125 = 2 + 3 \cdot 1^2, exactly as the identity says. The right leaf holds 10,11,1210, 11, 12, with mean 1111 and SSE 22.

Squared-error split

Definition Let a node hold a set SS of n≥2n \ge 2 training rows with target mean yˉS\bar{y}_S, and write SSE⁡(S)=∑i∈S(yi−yˉS)2\operatorname{SSE}(S) = \sum_{i \in S} (y_i - \bar{y}_S)^2. A candidate split xj≤tx_j \le t divides SS into a left part LL and a right part RR, both nonempty, with nLn_L and nRn_R rows and means yˉL\bar{y}_L and yˉR\bar{y}_R. The squared-error split criterion chooses, among the valid candidates, the one with the smallest total SSE⁡(L)+SSE⁡(R)\operatorname{SSE}(L) + \operatorname{SSE}(R), each child measured around its own mean, with a fixed rule for ties. The reduction is

Δ=SSE⁡(S)−SSE⁡(L)−SSE⁡(R)=nLnRn(yˉL−yˉR)2,\begin{aligned} \Delta &= \operatorname{SSE}(S) - \operatorname{SSE}(L) \\[0.4em] &\quad - \operatorname{SSE}(R) \\[0.4em] &= \frac{n_L n_R}{n} (\bar{y}_L - \bar{y}_R)^2 , \end{aligned}

which is never negative and is zero exactly when the two child means are equal. Thresholds for a numerical feature are the midpoints between consecutive distinct sorted values of xjx_j in the node, so rows with equal values of xjx_j always go to the same child. Since the SSE of a child is its number of rows times the variance of its targets, computed with that number as divisor, the criterion is the same as minimizing the size-weighted average of the children’s variances, which some software calls the mean squared error criterion.

In plain words A good split makes each side close to its own average. The reduction formula shows what that rewards, two sides whose averages are far apart, and more so when both sides hold many rows.

Example The demand data have mean 77 and squared residuals 25,16,9,9,16,2525, 16, 9, 9, 16, 25, so the root has SSE 100100. The five midpoints give the following totals.

Threshold ttSSE of the childrenReduction
1.51.570703030
2.52.539.2539.2560.7560.75
3.53.5449696
4.54.539.2539.2560.7560.75
5.55.570703030

The threshold 3.53.5 wins. Its children have 33 rows each with means 33 and 1111, and the formula gives the same reduction, 3⋅36(3−11)2=1.5⋅64=96\tfrac{3 \cdot 3}{6}(3 - 11)^2 = 1.5 \cdot 64 = 96. At 2.52.5 the children are 2,32, 3 with mean 2.52.5 and 4,10,11,124, 10, 11, 12 with mean 9.259.25, and 2⋅46(2.5−9.25)2=60.75\tfrac{2 \cdot 4}{6}(2.5 - 9.25)^2 = 60.75.

Tree depth and minimum leaf size

Definition The depth of a node is the number of splits on the path from the root to it, so the root has depth 00, and the depth of a tree is the largest depth of its leaves. A tree of depth dd has at most 2d2^d leaves. A maximum depth dmax⁡d_{\max} stops growth at that depth. A minimum leaf size mmin⁡≥1m_{\min} \ge 1 makes a split valid only when each child receives at least mmin⁡m_{\min} training rows. A smaller maximum depth or a larger minimum leaf size gives a less flexible tree. With no depth limit and mmin⁡=1m_{\min} = 1, splitting can continue until every leaf has constant targets or holds a single distinct input. If no two training rows share an input with different targets, every leaf then has constant targets and the training SSE is 00. Both settings are hyperparameters, chosen with validation data or with cross-validation inside the training data and never with the test set.

In plain words The depth limit caps how many questions a prediction may ask, and the minimum leaf size caps how few rows a single answer may rest on. Both keep the tree from giving each unusual row a bin of its own. Zero training error from a fully grown tree shows only that every row found its own bin.

Example Grown greedily on the demand data, with ties going to the smaller threshold, the trees with maximum depth 00, 11, 22 and 33 have 11, 22, 44 and 66 leaves and training SSE 100100, 44, 11 and 00. At depth 22 the node with targets 2,3,42, 3, 4 can split at 1.51.5 or at 2.52.5 with the same SSE 0.50.5, and the tie rule picks 1.51.5. At depth 33 every row has its own leaf. Without a depth limit, a minimum leaf size of 22 or 33 gives the tree with the single split at 3.53.5, since that split is still the best valid one at the root and any split of a three-row leaf leaves a child with one row. A minimum leaf size of 44 allows no split at all, since six rows cannot fill two children of four.

Cost-complexity pruning

Definition Grow a large tree T0T_0 on the training data. A pruned subtree TT of T0T_0 comes from collapsing some internal nodes, each together with everything below it, into leaves that predict the mean of their training rows. For a complexity parameter α≥0\alpha \ge 0, cost-complexity pruning scores TT by

Rα(T)=SSE⁡(T)+α∣T∣,R_\alpha(T) = \operatorname{SSE}(T) + \alpha \lvert T \rvert ,

where SSE⁡(T)\operatorname{SSE}(T) is the training SSE summed over the leaves and ∣T∣\lvert T \rvert is the number of leaves. A leaf is worth adding only if it lowers the training SSE by more than α\alpha. For every α\alpha there is a smallest subtree that minimizes RαR_\alpha, and as α\alpha grows these subtrees are nested, each contained in the ones for smaller values, reaching the root alone at the latest when α\alpha exceeds the SSE of the root. Weakest-link pruning finds them. For an internal node tt with branch TtT_t, let SSE⁡(t)\operatorname{SSE}(t) be the SSE of its rows around their mean, the value if tt were collapsed, and compute

g(t)=SSE⁡(t)−SSE⁡(Tt)∣Tt∣−1.g(t) = \frac{\operatorname{SSE}(t) - \operatorname{SSE}(T_t)}{\lvert T_t \rvert - 1} .

Collapse every node with the smallest value of gg, recompute and repeat. The successive smallest values are the values of α\alpha at which the best subtree changes. Pruning uses only training data to produce the candidates, and α\alpha is then chosen by validation error or by cross-validation inside the training data, never by the test set. On this page α\alpha is measured in squared target units per leaf. Software that measures training error as SSE⁡/n\operatorname{SSE}/n reaches the same tree with α/n\alpha/n in place of α\alpha.

In plain words Pruning grows a big tree and then cuts back the branches that do not pay for themselves. The parameter α\alpha is the price of one leaf. Each value of α\alpha picks one tree from a short list of nested candidates, and validation data, not training error, decides which value to use.

Example In the fully grown six-leaf demand tree, the two nodes holding the targets 3,43, 4 and 11,1211, 12 have g=0.5g = 0.5, the two three-row nodes have g=1g = 1 and the root has g=20g = 20. Collapsing the two weakest nodes leaves the four-leaf tree, in which the three-row nodes have g=(2−0.5)/1=1.5g = (2 - 0.5)/1 = 1.5. Collapsing those leaves the one-split tree, whose root has g=100−4=96g = 100 - 4 = 96. Four constructed validation rows, at temperatures 1.251.25, 2.752.75, 4.254.25 and 5.755.75 with demand 3,3,11,113, 3, 11, 11, took no part in fitting or pruning.

Values of α\alphaLeavesTraining SSEValidation MSE
[0,0.5)[0, 0.5)660011
[0.5,1.5)[0.5, 1.5)44110.6250.625
[1.5,96)[1.5, 96)224400
[96,∞)[96, \infty)111001001616

At α=2\alpha = 2, for instance, the root, the one-split tree and the full tree score 102102, 88 and 1212. Validation picks the one-split tree, although the full tree has the smallest training error. Its validation error of 00 comes from four deliberately chosen rows, and because those rows made the choice, an honest estimate of the chosen tree’s error needs data that played no part in it.

Check yourself

  1. A leaf holds the training targets 4,6,114, 6, 11. Find its prediction and its SSE, and check that the median 66 does worse. A proposed split would make the leaves {4,6}\{4, 6\} and {11}\{11\}. Does the cost-complexity criterion keep the leaf or split it at α=10\alpha = 10 and at α=30\alpha = 30?
Show the answer

The prediction is the mean (4+6+11)/3=7(4 + 6 + 11)/3 = 7. The residuals are −3,−1,4-3, -1, 4, so the SSE is 9+1+16=269 + 1 + 16 = 26. The constant 66 gives 4+0+25=294 + 0 + 25 = 29, which matches 26+3(7−6)226 + 3(7 - 6)^2 from the identity.

After the split, the leaf {4,6}\{4, 6\} has mean 55 and SSE 22, and the leaf {11}\{11\} has SSE 00, so the reduction is 2424. Keeping one leaf scores 26+α26 + \alpha and splitting scores 2+2α2 + 2\alpha. At α=10\alpha = 10 these are 3636 and 2222, so the split stays. At α=30\alpha = 30 they are 5656 and 6262, so the leaf is kept. The split pays exactly when its reduction 2424 exceeds the price α\alpha of the extra leaf, and at α=24\alpha = 24 the two scores tie and the smaller tree is kept.

  1. Constructed training rows have x=1,2,3,4,5x = 1, 2, 3, 4, 5 and y=1,2,3,4,20y = 1, 2, 3, 4, 20. Find the best squared-error split with no minimum leaf size and with a minimum leaf size of 22. What does each one-split tree predict at x=10x = 10?
Show the answer

The root mean is 66 and the root SSE is 25+16+9+4+196=25025 + 16 + 9 + 4 + 196 = 250. The four thresholds give these totals for the two children.

Threshold ttLeft targetsRight targetsSSE of the children
1.51.5112,3,4,202, 3, 4, 20218.75218.75
2.52.51,21, 23,4,203, 4, 20182.5182.5
3.53.51,2,31, 2, 34,204, 20130130
4.54.51,2,3,41, 2, 3, 4202055

With no minimum leaf size the split at 4.54.5 wins, with a reduction of 245245, and its right leaf holds the single row with target 2020. A minimum leaf size of 22 rules out 1.51.5 and 4.54.5, and the split at 3.53.5 wins with total 130130, giving the leaf means 22 and 1212. At x=10x = 10 the first tree predicts 2020 and the second 1212, since each continues its rightmost leaf. Neither tree is shown to be better by these numbers. The first rests a whole region on one row, which may be a recording error or a real effect, and the second blends that row with a very different one. Validation data, together with a check of how the value 2020 was recorded, should decide.

  1. Two analysts choose α\alpha for the demand tree. One picks the value whose pruned tree has the smallest training SSE. The other picks the value whose pruned tree has the smallest test MSE and reports that MSE as the error to expect. What goes wrong in each case, and what is an honest procedure?
Show the answer

Collapsing a node never lowers the training SSE. The branch below the node could give all its leaves the node’s mean and match the collapsed SSE, and with each leaf’s own mean it does at least as well. So the first analyst always ends up with the largest tree on the path, here the six-leaf tree with training SSE 00, which is the pruned tree for every α\alpha below 0.50.5. Training error cannot measure the cost of complexity.

The second analyst uses the test set to make a choice. The reported MSE is then the smallest of several test scores, so it tends to understate the error of the chosen tree on new data, and no untouched data remain for an honest assessment.

An honest procedure keeps the test set aside. Compute the pruning path on the training data and choose α\alpha, together with any depth or leaf size limits, by the error on a separate validation set or by cross-validation within the training data, recomputing the path inside each fold. Then refit on all the training data, prune at the chosen α\alpha, and evaluate that one tree once on the test set. If the rows are grouped or ordered in time, the validation splits must respect that structure too.

How this lesson connects