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 from a feature vector . Like a classification tree, it is a rooted binary tree whose internal nodes hold tests such as , which send an input to the left child when the test holds and to the right child otherwise, and whose leaves define disjoint regions that together cover feature space. Leaf holds a constant , and the prediction is
When the tree is fitted with squared error, each constant is the mean of the training targets in . 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 with a demand in units sold.
| Temperature | ||||||
|---|---|---|---|---|---|---|
| Demand |
The tree with the single test predicts when and when . A temperature of exactly goes left and gets , while gets . Outside the data, the temperature gets and the temperature gets , 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 training targets with mean . For every constant ,
because the cross term is zero. The last term is positive unless , so the leaf mean is the unique constant with the smallest sum of squared errors in the leaf, and that smallest sum, , 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 holds the targets . Its mean is , its residuals are , and its SSE is . The constant gives squared errors , a total of , exactly as the identity says. The right leaf holds , with mean and SSE .
Squared-error split
Definition Let a node hold a set of training rows with target mean , and write . A candidate split divides into a left part and a right part , both nonempty, with and rows and means and . The squared-error split criterion chooses, among the valid candidates, the one with the smallest total , each child measured around its own mean, with a fixed rule for ties. The reduction is
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 in the node, so rows with equal values of 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 and squared residuals , so the root has SSE . The five midpoints give the following totals.
| Threshold | SSE of the children | Reduction |
|---|---|---|
The threshold wins. Its children have rows each with means and , and the formula gives the same reduction, . At the children are with mean and with mean , and .
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 , and the depth of a tree is the largest depth of its leaves. A tree of depth has at most leaves. A maximum depth stops growth at that depth. A minimum leaf size makes a split valid only when each child receives at least training rows. A smaller maximum depth or a larger minimum leaf size gives a less flexible tree. With no depth limit and , 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 . 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 , , and have , , and leaves and training SSE , , and . At depth the node with targets can split at or at with the same SSE , and the tie rule picks . At depth every row has its own leaf. Without a depth limit, a minimum leaf size of or gives the tree with the single split at , 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 allows no split at all, since six rows cannot fill two children of four.
Cost-complexity pruning
Definition Grow a large tree on the training data. A pruned subtree of 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 , cost-complexity pruning scores by
where is the training SSE summed over the leaves and is the number of leaves. A leaf is worth adding only if it lowers the training SSE by more than . For every there is a smallest subtree that minimizes , and as grows these subtrees are nested, each contained in the ones for smaller values, reaching the root alone at the latest when exceeds the SSE of the root. Weakest-link pruning finds them. For an internal node with branch , let be the SSE of its rows around their mean, the value if were collapsed, and compute
Collapse every node with the smallest value of , recompute and repeat. The successive smallest values are the values of at which the best subtree changes. Pruning uses only training data to produce the candidates, and is then chosen by validation error or by cross-validation inside the training data, never by the test set. On this page is measured in squared target units per leaf. Software that measures training error as reaches the same tree with in place of .
In plain words Pruning grows a big tree and then cuts back the branches that do not pay for themselves. The parameter is the price of one leaf. Each value of 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 and have , the two three-row nodes have and the root has . Collapsing the two weakest nodes leaves the four-leaf tree, in which the three-row nodes have . Collapsing those leaves the one-split tree, whose root has . Four constructed validation rows, at temperatures , , and with demand , took no part in fitting or pruning.
| Values of | Leaves | Training SSE | Validation MSE |
|---|---|---|---|
At , for instance, the root, the one-split tree and the full tree score , and . Validation picks the one-split tree, although the full tree has the smallest training error. Its validation error of 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
- A leaf holds the training targets . Find its prediction and its SSE, and check that the median does worse. A proposed split would make the leaves and . Does the cost-complexity criterion keep the leaf or split it at and at ?
Show the answer
The prediction is the mean . The residuals are , so the SSE is . The constant gives , which matches from the identity.
After the split, the leaf has mean and SSE , and the leaf has SSE , so the reduction is . Keeping one leaf scores and splitting scores . At these are and , so the split stays. At they are and , so the leaf is kept. The split pays exactly when its reduction exceeds the price of the extra leaf, and at the two scores tie and the smaller tree is kept.
- Constructed training rows have and . Find the best squared-error split with no minimum leaf size and with a minimum leaf size of . What does each one-split tree predict at ?
Show the answer
The root mean is and the root SSE is . The four thresholds give these totals for the two children.
| Threshold | Left targets | Right targets | SSE of the children |
|---|---|---|---|
With no minimum leaf size the split at wins, with a reduction of , and its right leaf holds the single row with target . A minimum leaf size of rules out and , and the split at wins with total , giving the leaf means and . At the first tree predicts and the second , 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 was recorded, should decide.
- Two analysts choose 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 , which is the pruned tree for every below . 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 , 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 , 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.