Tree-Based Methods: Classification and Regression Trees (CART)
Anchor (Master): Hastie, Tibshirani & Friedman 2009 The Elements of Statistical Learning 2e §9.2 and Ch. 10 (the instability of trees as motivation for ensembling); Breiman, Friedman, Olshen & Stone 1984 Ch. 3 (optimality of cost-complexity pruning) and Ch. 4 (right-sized trees by cross-validation); Lin & Jeon 2006 J. Amer. Statist. Assoc. 101 (random forests as adaptive nearest-neighbour / potential-nearest-neighbour estimators)
Intuition Beginner
A decision tree predicts by asking a short series of yes/no questions about a case and reading off an answer at the end. Is the applicant's income above 50 thousand? If yes, is their account more than three years old? If no, how many late payments do they have? Each answer sends you down one branch, and when you reach a leaf at the bottom you stop and give the prediction stored there. The whole model is a flowchart you could follow with pencil and paper.
The questions are not chosen by hand. The tree is grown from data by a simple rule: at each step, look at every feature and every place you could split it, and pick the single split that best separates the cases into more similar groups. "More similar" means closer in the value you are trying to predict. For a number you want to predict, like a house price, similar means the prices in each group are close together. For a category, like spam or not spam, similar means each group is as pure as possible, mostly one class.
A tree gives a piecewise prediction: it carves the space of features into rectangular boxes, and inside each box it predicts one fixed value, the average of the training cases that fell in that box. The boxes are the leaves. Big leaves make a coarse, steady model; many small leaves make a detailed, jumpy one.
The appeal of trees is that you can read them. A doctor can follow the path that led to a diagnosis and check whether each question makes sense. The cost is that a single tree is twitchy: change a few training cases and the top split can flip, redrawing every box below it. That twitchiness is the reason people average many trees together, which later units take up.
Visual Beginner
Picture the two-feature plane with income across the bottom and age up the side. The first split draws a vertical line at income equals 50; the left part splits again with a horizontal line at age equals 30; the right part splits at income equals 80. You end up with four rectangular boxes, and the tree predicts one value in each box.
| Where you are | Question asked | What happens |
|---|---|---|
| Root | income above 50? | go left if no, right if yes |
| Left child | age above 30? | reach a leaf either way |
| Right child | income above 80? | reach a leaf either way |
| Any leaf | none | predict the box's stored value |
Reading the flowchart and reading the carved plane are two views of one model: every path from the root to a leaf names one rectangular box, and the prediction is constant inside it.
Worked example Beginner
Six houses, each with a lot size (in thousands of square feet) and a sale price (in thousands of dollars). We grow one split of a regression tree.
| House | Lot size | Price |
|---|---|---|
| 1 | 2 | 150 |
| 2 | 3 | 160 |
| 3 | 4 | 200 |
| 4 | 6 | 310 |
| 5 | 7 | 330 |
| 6 | 8 | 350 |
Step 1. The do-nothing baseline. The average price of all six is . The total squared error around this single average is .
Step 2. Try splitting at lot size 5. Left group is houses 1, 2, 3 with prices 150, 160, 200, averaging . Right group is houses 4, 5, 6 with prices 310, 330, 350, averaging .
Step 3. Score this split by its squared error. Left error is . Right error is . The split's total is .
Step 4. Compare. The split drops the squared error from 40600 to 2200, an enormous reduction. A scan over the other candidate split points (between 3 and 4, between 6 and 7, and so on) would find none that beats this one, so the tree splits at lot size 5.
What this tells us: the tree found, on its own, that small lots and large lots are two clean groups, and it predicts 170 for small lots and 330 for large ones. The split point was chosen by the one number it cares about, the within-group squared error, with no formula supplied in advance.
Check your understanding Beginner
Formal definition Intermediate+
Let have joint law on with , and write a training sample . A tree encodes a recursive binary partition of into axis-aligned rectangles , one per leaf (terminal node), where is the number of leaves. Each internal node carries a split sending a case to the left child if and to the right child otherwise (for an ordered predictor and threshold ); for a categorical predictor a split is a partition of its level set into two groups. A node owns the subset reached by its ancestral splits and the index set of size .
Regression tree. The estimator is piecewise constant, , and on a fixed partition the within-leaf sum of squared errors is minimised at the leaf mean . The node impurity is the within-node mean squared error .
Classification tree. With classes, let be the node class proportions and the majority class. Three node impurity measures are standard:
All three vanish at a pure node () and are maximal at the uniform distribution; Gini and entropy are strictly concave in the vector , while misclassification error is concave but piecewise linear.
Greedy split selection. For a node with impurity , a split into children with proportions , has impurity reduction
CART grows the tree by choosing, at each node, the split over all predictors and all candidate thresholds, then recursing on the children, stopping when nodes are pure or fall below a minimum size.
Cost-complexity. For and a subtree obtained by pruning, the cost-complexity criterion is
the total node cost penalised by times the number of leaves. Writing for the resubstitution cost, .
The notation here — for indicators, for a leaf mean, for node class proportions, for leaf count, and — is recorded in _meta/NOTATION.md.
Counterexamples to common slips Intermediate+
"Misclassification error is the best criterion to grow by, since it is what we ultimately care about." Misclassification error is insensitive: a split that moves a node from to two children at and leaves the misclassification impurity weighted sum unchanged at , so the split looks worthless, whereas Gini and entropy both record a strict gain. The strict concavity of Gini and entropy is what makes any non-degenerate split register progress; misclassification error is reserved for pruning, where the objective genuinely is error.
"A bigger tree always has lower cost, so growth never overshoots." Resubstitution cost decreases monotonically with growth and is minimised by the fully grown tree, which typically interpolates the training data. It is the complexity penalty , not , that creates an interior optimum; without it the criterion selects the overfit maximal tree.
"Splitting a -level categorical predictor requires examining all binary groupings." For two-class problems (and for squared-error regression), ordering the levels by their mean response and splitting on that order finds the optimal binary partition in comparisons — the Fisher/Breiman ordering result — so the exponential search is avoided in the cases that matter most.
Key theorem with proof Intermediate+
The signature structural fact is that cost-complexity pruning produces, from a single grown tree, a finite nested sequence of subtrees indexed by a finite increasing sequence of penalties — the object cross-validation then searches over. Everything operational about CART model selection rests on this.
Theorem (weakest-link pruning; Breiman-Friedman-Olshen-Stone). Fix a grown tree . For each there is a unique smallest subtree (smallest in number of leaves) minimising . As increases from to the minimisers are nested,
so the entire family collapses to a finite chain obtained by repeatedly pruning the weakest-link branch. [Breiman 1984]
Proof. For a node of , let be the branch rooted at and the subtree got by collapsing to a leaf. Define the resubstitution cost of a single node and of its branch . Because each split chooses children minimising the weighted child impurity, growing never increases cost: , with leaves under . The cost of keeping the branch versus collapsing it crosses as rises. Collapsing is at least as cheap under when
the per-extra-leaf cost saving of the branch. The function is the link strength of node : small marks a branch that buys little accuracy per leaf, the weakest link.
Define over internal nodes of , and let be with every branch attaining this minimum collapsed to its root. For no collapse lowers , so ; at the collapsed tree has cost equal to and fewer leaves, hence is the *smallest* minimiser. Recomputing on and taking its minimum gives and ; iterating terminates at the root. Uniqueness of the smallest minimiser at each follows because if two distinct subtrees both minimise , their intersection (the larger common pruned subtree) also minimises it and is smaller, so the minimiser of least size is unique. The breakpoints are finite because each step removes at least one node from a finite tree.
Bridge. This theorem is the foundational reason CART can be tuned with a single one-dimensional cross-validation: instead of searching the astronomically large space of subtrees of , one searches the finite chain , and this is exactly the move that makes pruning practical. The weakest-link quantity generalises the simple per-split impurity drop used during growth — growth maximises the drop locally, pruning charges the accumulated drop of a whole branch against its leaf budget — so the bridge is that the same cost-versus-complexity ledger drives both phases of CART, read forward to grow and backward to prune. The nested sequence appears again in the cross-validation step of the Advanced results, where each is scored on held-out folds and the one-standard-error rule picks the sparsest tree within noise of the best; it builds toward the instability analysis that motivates averaging trees in 45.08.05 and 45.08.06, since the very greediness that makes the chain finite is what makes each individual tree high-variance.
Exercises Intermediate+
Advanced results Master
Cost-complexity pruning hands cross-validation a finite chain; the remaining theory concerns how to select within it, how categorical and missing data are absorbed, and the variance that single trees pay for their adaptivity — the variance that ensembling spends the rest of the chapter removing.
Theorem 1 (cross-validated selection and the one-standard-error rule). Grow on the full sample and form the penalty chain with geometric midpoints . For each fold of a -fold partition, grow a tree on the complement and prune it to the subtree minimising , then estimate its risk on fold ; averaging over folds gives a cross-validation risk with standard error . The one-standard-error rule selects the sparsest tree whose lies within of the minimum, [Breiman 1984]. The rule trades a statistically indistinguishable increase in estimated risk for a smaller, more stable, more interpretable tree, and is the CART default.
Theorem 2 (optimal categorical splits by ordering). For a two-class () classification problem with Gini or entropy impurity, and for squared-error regression, the optimal binary split of a categorical predictor with levels is found by ordering the levels by the response statistic — the proportion in class 1 for two-class problems, the mean response for regression — and considering only the splits that respect this order [Breiman 1984]. This reduces the search from subsets to thresholds. The result fails for classes, where the full subset search (or the twoing criterion, which groups classes into two superclasses at each node) is required and the favouring of high-cardinality categorical predictors becomes a known bias of impurity-based splitting.
Theorem 3 (instability and the bias-variance decomposition of trees). A fully grown tree is a low-bias, high-variance estimator: its leaves interpolate, so squared bias is small, while the variance is large because the partition itself is a discontinuous function of the data — a perturbation that flips a high split relocates every descendant cell. For a prediction the expected squared error decomposes as , and for trees the dominant reducible term is the variance [Hastie 2009]. Averaging identically distributed trees with pairwise prediction correlation has variance , so bagging (which lowers by averaging) and random forests (which additionally lower by decorrelating splits) attack exactly the term that dominates a single tree's error; boosting instead grows shallow, high-bias trees and reduces bias by sequential reweighting.
Theorem 4 (trees as adaptive nearest-neighbour estimators). Write a tree prediction as a weighted average of training responses, , where and is the leaf containing ; the weights are supported on the leaf neighbourhood of and sum to one. A tree is thus a nearest-neighbour rule whose neighbourhoods are data-adaptive axis-aligned rectangles rather than fixed Euclidean balls, with the splits choosing both the shape and the local feature directions [Lin 2006]. Averaging trees over bootstrap samples or random feature subsets yields a forest weight , the potential-nearest-neighbour weighting, and casts the consistency of forests as a question about whether these adaptive neighbourhoods shrink while still containing enough points — the bridge between partition estimators and local averaging.
Synthesis. CART is one ledger — cost against complexity — read in two directions, and the foundational reason the method coheres is that the same per-leaf accounting that greedily grows the tree by maximising local impurity drop is what prunes it by charging each branch's accumulated drop against its leaf budget through the link strength . The weakest-link theorem is exactly this duality made finite: it collapses the uncountable family of penalised subtrees to a nested chain, and putting these together with cross-validation and the one-standard-error rule turns model selection into a one-dimensional search. The impurity choice is not cosmetic — Gini and entropy are strictly concave, which is the central insight that every non-degenerate split registers a strictly positive gain (Exercise 6), whereas piecewise-linear misclassification error is blind to splits that do not flip the majority and is demoted to the pruning objective. The instability this greedy, discontinuous partitioning buys generalises into the variance term that dominates a tree's error, and is dual to the averaging downstream: bagging and random forests in 45.08.05 reduce that variance while boosting in 45.08.06 reduces the bias of deliberately stunted trees. The nearest-neighbour reading is the bridge that unifies the picture — a tree is an adaptive local-averaging estimator whose neighbourhoods are its leaves, so the partition theory of CART, the consistency theory of forests, and classical -nearest-neighbours are three views of one weighted-average estimator, differing only in how the weights are chosen.
Full proof set Master
Proposition 1 (leaf mean minimises within-node SSE; impurity is node variance). For a regression node , , and the minimum equals .
Proof. The objective has , vanishing at , and , so is the unique minimiser. Substituting, .
Proposition 2 (non-negativity of impurity reduction for concave impurity). Let the node impurity be for a concave on the probability simplex. For any split of node into with weights and class vectors satisfying , the reduction ; if is strictly concave, equality holds iff .
Proof. Apply Jensen's inequality to the concave and the two-point law placing mass at and at , whose mean is : , i.e. . For strictly concave , Jensen is strict unless the support is a single point, ; combined with this gives . The Gini index and entropy are strictly concave, so every non-degenerate split strictly reduces them; misclassification is concave but not strictly, so degenerate-looking splits can leave it unchanged.
Proposition 3 (weakest-link pruning: existence, uniqueness, nestedness). For a grown tree and each there is a unique smallest subtree minimising , and these are nested in , obtained by iteratively collapsing the node of least link strength .
Proof. For a node , comparing the branch with its collapse under : keeping the branch is preferred while , i.e. while , and collapsing is (weakly) preferred once . Hence below no collapse improves and . At , collapsing every node attaining the minimum leaves unchanged while reducing , so the collapsed tree is a minimiser with strictly fewer leaves than ; it is the smallest minimiser because any subtree with even one of the weakest branches retained has the same but more leaves, and any subtree collapsing a non-weakest branch has strictly larger . Uniqueness of the smallest minimiser at general : if both minimise , then on any node where they differ one is a collapse of the other and both have equal , so their common pruned subtree also minimises and has no more leaves than either; the minimiser of least leaf count is therefore unique. Recomputing on and repeating yields (link strengths only rise after the weakest links are removed) and ; each step deletes at least one node, so after finitely many steps the chain reaches the root, giving the finite nested sequence.
Proposition 4 (variance of an average of correlated trees). Let be identically distributed tree predictions at a point with common variance and pairwise correlation . The bagged predictor has variance .
Proof. . As the second term vanishes, leaving the floor : averaging removes the part of the variance that decorrelation, not count, controls — the quantitative case for random forests over plain bagging.
Connections Master
The squared-error splitting criterion and the leaf-mean fit specialise the least-squares regression developed in
26.06.01: a regression tree is a sequence of plain constant-mean regressions on adaptively chosen rectangular subsamples, and the within-node SSE the tree minimises is the residual sum of squares of that local constant fit, so the foundational accounting is the same orthogonality-of-residuals identity, applied piecewise rather than globally.The risk a tree estimates is the conditional expectation of the joint law set up in
37.01.01; the leaf average is the empirical conditional mean over the leaf cell, and consistency of trees and forests is the statement that these adaptive cell-averages converge to the regression function as the cells shrink while retaining points — the partition-estimator face of the law of large numbers on the probability space of that unit.Trees are the base learners of the ensemble methods in
45.08.05(bagging and random forests) and45.08.06(boosting): the high-variance, low-bias profile proved in Theorem 3 and Proposition 4 is exactly the regime where bootstrap averaging and split-decorrelation pay off, and the shallow stumps boosting reweights are pruned trees at the small- end of the cost-complexity chain, so this unit supplies the object those units combine.The nearest-neighbour reading of Theorem 4 ties CART to the adaptive local-averaging estimators studied in nonparametric regression: a tree, a forest, and a -nearest-neighbour rule are one weighted-average estimator differing only in how the data-dependent weights are formed, which is the central insight that lets the consistency theory of partition estimators and of local averaging be developed together.
Historical & philosophical context Master
Recursive partitioning predates CART — automatic interaction detection (AID) of Morgan and Sonquist (1963) split survey data by variance reduction, and THAID and CHAID followed in the 1970s — but these early programs grew trees without a principled stopping rule and were criticised for overfitting and for greedy selection bias. The decisive synthesis is the 1984 monograph of Leo Breiman, Jerome Friedman, Richard Olshen, and Charles Stone, Classification and Regression Trees [Breiman 1984], which introduced the impurity-reduction splitting criterion, the Gini and twoing rules, surrogate splits for missing data, and — the technical heart — cost-complexity pruning with its weakest-link algorithm and cross-validated tree selection, replacing ad-hoc stopping with grow-then-prune.
The statistical-learning recasting in Hastie, Tibshirani, and Friedman's Elements of Statistical Learning (2001; 2nd ed. 2009) [Hastie 2009] placed CART within the bias-variance framework and made the instability of single trees the explicit motivation for the ensemble methods Breiman himself went on to develop — bagging (1996) and random forests (2001). The identification of trees and forests as adaptive nearest-neighbour estimators is due to Lin and Jeon (2006) [Lin 2006], who introduced the potential-nearest-neighbour weighting that subsequent consistency proofs for random forests rely on. The interpretability-versus-accuracy tension that runs through tree methodology — a single tree is readable but unstable, an averaged forest is accurate but opaque — remains the defining trade-off of the family and the reason both single trees and their ensembles persist side by side in applied practice.
Bibliography Master
@book{breiman1984cart,
author = {Breiman, Leo and Friedman, Jerome H. and Olshen, Richard A. and Stone, Charles J.},
title = {Classification and Regression Trees},
publisher = {Wadsworth},
address = {Belmont, CA},
year = {1984}
}
@book{hastie2009esl,
author = {Hastie, Trevor and Tibshirani, Robert and Friedman, Jerome},
title = {The Elements of Statistical Learning: Data Mining, Inference, and Prediction},
edition = {2},
publisher = {Springer},
year = {2009}
}
@article{morgansonquist1963,
author = {Morgan, James N. and Sonquist, John A.},
title = {Problems in the analysis of survey data, and a proposal},
journal = {Journal of the American Statistical Association},
volume = {58},
number = {302},
pages = {415--434},
year = {1963}
}
@article{breiman1996bagging,
author = {Breiman, Leo},
title = {Bagging predictors},
journal = {Machine Learning},
volume = {24},
number = {2},
pages = {123--140},
year = {1996}
}
@article{breiman2001randomforests,
author = {Breiman, Leo},
title = {Random forests},
journal = {Machine Learning},
volume = {45},
number = {1},
pages = {5--32},
year = {2001}
}
@article{linjeon2006,
author = {Lin, Yi and Jeon, Yongho},
title = {Random forests and adaptive nearest neighbors},
journal = {Journal of the American Statistical Association},
volume = {101},
number = {474},
pages = {578--590},
year = {2006}
}