Simple Algorithm
Machine Learning · Convergence · 28 min

CART in Depth

Split criteria, cost-complexity pruning, categorical features and missing values.

CART rests on three small theorems: concave impurities never increase under a split, the subtrees that minimize cost-complexity are nested, and the best binary split of a categorical feature lies along one ordering of its levels. This lesson proves all three, explains surrogate splits for missing values, and sets CART beside ID3, C4.5 and the gain ratio.

What you’ll learn

  • Prove that concave impurities never increase under a split, characterize when strictly concave ones stay flat, and explain why misclassification error fails as a growing criterion.
  • Prove that cost-complexity pruning yields a finite nested sequence of optimal subtrees computed by weakest-link pruning, and select one by cross-validation.
  • Prove the ordering theorem for binary splits of a categorical feature in regression and two-class problems, and explain why it fails for three or more classes.
  • Explain surrogate splits, ID3's bias toward many-valued attributes, and C4.5's gain ratio.

Concave impurities, and why misclassification fails

DerivationProof, and the failure of misclassification error
  1. p^t=wL p^tL+wR p^tR\hat{\mathbf p}_t=w_L\,\hat{\mathbf p}_{t_L}+w_R\,\hat{\mathbf p}_{t_R}

    Counting examples class by class, the parent's proportions are the mixture of the children's, with weights wL,wR>0w_L,w_R>0 (both children are nonempty):

  2. ϕ(wLp^tL+wRp^tR) ≥ wL ϕ(p^tL)+wR ϕ(p^tR)\phi\big(w_L\hat{\mathbf p}_{t_L}+w_R\hat{\mathbf p}_{t_R}\big)\ \ge\ w_L\,\phi(\hat{\mathbf p}_{t_L})+w_R\,\phi(\hat{\mathbf p}_{t_R})

    Concavity is exactly Jensen's inequality for two points, and it gives Δi≥0\Delta i\ge0; strict concavity makes the inequality strict unless the two points coincide.

  3. ∇2(1−∑kpk2)=−2I,∇2(−∑kpklog⁡2pk)=−1ln⁡2diag⁡(1p1,…,1pK)\nabla^2\Big(1-\sum_kp_k^2\Big)=-2I,\qquad\nabla^2\Big(-\sum_kp_k\log_2p_k\Big)=-\frac1{\ln2}\diag\Big(\frac1{p_1},\dots,\frac1{p_K}\Big)

    Gini and entropy are strictly concave on the simplex: their Hessians are negative definite in the interior (entropy measured in bits).

  4. wL(1−p^tL(c))+wR(1−p^tR(c))=1−p^t(c)⟹Δi=0w_L\big(1-\hat p_{t_L}(c)\big)+w_R\big(1-\hat p_{t_R}(c)\big)=1-\hat p_t(c)\quad\Longrightarrow\quad\Delta i=0

    Misclassification error 1−max⁡kpk1-\max_kp_k is concave (a minimum of affine functions) but affine wherever the majority class is fixed. If both children keep the parent's majority class cc, the weighted error equals the parent's, whatever happens to the other classes:

For example, a node with 70 examples of class 1 and 30 of class 2 has misclassification error 0.30. Splitting off 35 class-1 examples into a pure child leaves (35,30)(35,30), still with a class-1 majority: the weighted error is 0.65⋅3065=0.300.65\cdot\frac{30}{65}=0.30, a decrease of exactly zero, although half of the majority class is now settled for good. Gini falls from 0.420 to 0.323, and entropy from 0.881 to 0.647 bits.

Check your understanding

A node holds 80 examples of class 1 and 20 of class 2. Which split leaves the misclassification error unchanged?

  1. (40,0)(40,0) and (40,20)(40,20)
  2. (75,5)(75,5) and (5,15)(5,15)
  3. (10,20)(10,20) and (70,0)(70,0)
  4. (70,5)(70,5) and (10,15)(10,15)
Show the answer

A. (40,0)(40,0) and (40,20)(40,20)

In (40,0)(40,0) and (40,20)(40,20) both children keep the class-1 majority, so the errors are 0+20=200+20=20, the same as the parent's 20. In each of the other splits one child switches to a class-2 majority, and the error count falls to 10, 10 and 15 respectively. Gini still rewards the first split: it falls from 0.32 to 0.6⋅2⋅23⋅13≈0.2670.6\cdot2\cdot\tfrac23\cdot\tfrac13\approx0.267.

Concavity also governs how fast greedy growth can drive the training error down. Kearns & Mansour (1999) analyzed top-down induction under a weak-learning assumption: at every node, some allowed split function has error at most 1/2−γ1/2-\gamma on a suitably reweighted node distribution. Under it, greedy growth reaches training error ε\varepsilon with trees of size (1/ε)O(1/(γ2ε2))(1/\varepsilon)^{O(1/(\gamma^2\varepsilon^2))} using the Gini criterion and (1/ε)O(log⁡(1/ε)/γ2)(1/\varepsilon)^{O(\log(1/\varepsilon)/\gamma^2)} using entropy, as in C4.5; their new criterion 2p(1−p)2\sqrt{p(1-p)}, more sharply curved near pure nodes, needs only (1/ε)O(1/γ2)(1/\varepsilon)^{O(1/\gamma^2)}, polynomial in 1/ε1/\varepsilon. They also note why misclassification error offers no such guarantee: its decrease is nonzero only when the two children have different majority classes, which the weak-learning assumption does not imply. In this precise sense top-down tree growth is a boosting algorithm (Boosting).

Cost-complexity pruning: the nesting theorem

Fix a large tree T0T_0. A pruned subtree T⪯T0T\preceq T_0 keeps the root and, at each kept node, either both children or neither. Write T~\tilde T for its leaves, R(t)R(t) for the training error of node tt if it were a leaf, counted as a fraction of all nn examples so that leaf errors add up, and R(T)=∑t∈T~R(t)R(T)=\sum_{t\in\tilde T}R(t). The cost-complexity is Rα(T)=R(T)+α∣T~∣R_\alpha(T)=R(T)+\alpha|\tilde T|.

DerivationProof sketch: dynamic programming on the tree
  1. Ft(α)=min⁡{R(t)+α, FtL(α)+FtR(α)}F_t(\alpha)=\min\Big\{R(t)+\alpha,\ F_{t_L}(\alpha)+F_{t_R}(\alpha)\Big\}

    Let TtT_t be the branch of T0T_0 rooted at tt and Ft(α)=min⁡T′⪯TtRα(T′)F_t(\alpha)=\min_{T'\preceq T_t}R_\alpha(T'). Because RαR_\alpha adds over leaves, an optimal pruning of TtT_t either collapses tt or prunes the two child branches optimally and independently (for a leaf, Ft=R(t)+αF_t=R(t)+\alpha):

  2. Dt(α)=FtL(α)+FtR(α)−R(t)−α ≥ 0D_t(\alpha)=F_{t_L}(\alpha)+F_{t_R}(\alpha)-R(t)-\alpha\ \ge\ 0

    Each FtF_t is the minimum of finitely many affine functions R(T′)+α∣T~′∣R(T')+\alpha|\tilde T'|, one per subtree: concave and piecewise linear in α\alpha, with one-sided slopes equal to leaf counts, so at least 1. Collapsing tt is (weakly) optimal exactly when the advantage of collapsing is nonnegative:

  3. Dt(α′)−Dt(α) ≥ α′−α > 0(α′>α)D_t(\alpha')-D_t(\alpha)\ \ge\ \alpha'-\alpha\ >\ 0\qquad(\alpha'>\alpha)

    The two child terms have slopes of at least 1 each, so DtD_t has slope at least 2−1=12-1=1 everywhere: it is strictly increasing. The set of α\alpha at which tt is collapsed in the smallest optimal subtree (ties broken toward collapsing) is therefore an interval [at,∞)[a_t,\infty): once pruned, a node stays pruned.

  4. R(t)+α=R(Tk,t)+α ∣T~k,t∣⟺α=gk(t)R(t)+\alpha=R(T_{k,t})+\alpha\,|\tilde T_{k,t}|\quad\Longleftrightarrow\quad\alpha=g_k(t)

    Induct from the leaves up: the smallest optimal subtree of TtT_t is {t}\{t\} when Dt(α)≥0D_t(\alpha)\ge0, and otherwise the union of its children's, which are nested in α\alpha by induction. So Tα′⪯TαT_{\alpha'}\preceq T_\alpha. Inside TαkT_{\alpha_k}, collapsing tt ties with keeping its branch exactly when R(t)+α=R(Tk,t)+α∣T~k,t∣R(t)+\alpha=R(T_{k,t})+\alpha|\tilde T_{k,t}|, which is the weakest-link formula; the smallest such α\alpha is the next breakpoint.

Think first A complete binary tree of depth DD has N(D)=N(D−1)2+1N(D)=N(D-1)^2+1 pruned subtrees, with N(0)=1N(0)=1: either collapse the root, or prune both depth-(D−1)(D-1) branches independently. How many does a depth-5 tree have, and how many of them can the weakest-link sequence visit?

N(1)=2N(1)=2, N(2)=5N(2)=5, N(3)=26N(3)=26, N(4)=677N(4)=677 and N(5)=458,330N(5)=458{,}330: doubly exponential growth. Yet the weakest-link sequence of a depth-5 tree has at most 32 members, since each step removes at least one of its 32 leaves. The nesting theorem turns an enormous search into a short path.

Selecting α\alpha. Cross-validation must prune each fold's own tree. For fold vv, grow T0(v)T_0^{(v)} on the other folds, prune it at the geometric midpoints βk=αkαk+1\beta_k=\sqrt{\alpha_k\alpha_{k+1}} of the full-data breakpoints, and score each pruned tree on fold vv. The βk\beta_k with the lowest average error selects TαkT_{\alpha_k} from the full-data sequence. The one-standard-error rule, introduced with CART, instead takes the smallest tree whose cross-validated error is within one standard error of the minimum: the curve is usually flat and noisy near its minimum, so a much smaller tree costs little accuracy.

Categorical features: the ordering theorem

DerivationProof for squared error and for two-class Gini
  1. SSES+SSESc=const+∑ℓ∈Snℓ(μℓ−mS)2+∑ℓ∉Snℓ(μℓ−mSc)2\mathrm{SSE}_S+\mathrm{SSE}_{S^c}=\text{const}+\sum_{\ell\in S}n_\ell(\mu_\ell-m_S)^2+\sum_{\ell\notin S}n_\ell(\mu_\ell-m_{S^c})^2

    The within-level variation is the same for every split, so minimizing SSES+SSESc\mathrm{SSE}_S+\mathrm{SSE}_{S^c} over sets of levels SS means minimizing the weighted within-group sum of squares of the level means. Treat level ℓ\ell as a point μℓ\mu_\ell of weight nℓn_\ell; let WSW_S and mSm_S be the total weight and weighted mean of group SS:

  2. Δ=wW′W′+w(μ−m′)2−wWW−w(μ−m)2\Delta=\frac{wW'}{W'+w}(\mu-m')^2-\frac{wW}{W-w}(\mu-m)^2

    Move one level (weight ww, mean μ\mu) from a group of weight WW and mean mm to the other group, of weight W′W' and mean m′m'. Removing a weighted point lowers a group's sum of squares by wWW−w(μ−m)2\frac{wW}{W-w}(\mu-m)^2, and adding one raises it by wW′W′+w(μ−m′)2\frac{wW'}{W'+w}(\mu-m')^2, so the move changes the objective by

  3. ∣μℓ−mS∣<∣μℓ−mSc∣for every ℓ∈S, and symmetrically for Sc\abs{\mu_\ell-m_S}<\abs{\mu_\ell-m_{S^c}}\quad\text{for every }\ell\in S,\ \text{and symmetrically for }S^c

    At an optimum no move helps, so Δ≥0\Delta\ge0. Since W′W′+w<1<WW−w\frac{W'}{W'+w}<1<\frac{W}{W-w}, this forces every level (barring exact ties) to lie strictly closer to its own group's mean than to the other's. The groups are separated by the midpoint (mS+mSc)/2(m_S+m_{S^c})/2, so each is a contiguous block of the sorted order. (A single-level group is trivially at its own mean.)

  4. wLGL+wRGR=2n(SSEL+SSER)(y∈{0,1})w_LG_L+w_RG_R=\frac2n\big(\mathrm{SSE}_L+\mathrm{SSE}_R\big)\qquad(y\in\{0,1\})

    For two classes, the Gini impurity 2p^(1−p^)2\hat p(1-\hat p) is twice the variance of a 0/1 label, so the weighted Gini of a split is 2/n2/n times its SSE on 0/1 labels, and the regression case applies with μℓ=p^(1∣ℓ)\mu_\ell=\hat p(1\mid\ell). Entropy needs a separate argument, given by Breiman et al. (1984).

Check your understanding

In a 3-class problem, a library sorts the 6 levels of a categorical feature by p^(class 1∣level)\hat p(\text{class }1\mid\text{level}) and scores only the 5 prefix splits. Is the Gini-optimal binary split guaranteed to be among them?

  1. Yes: the ordering theorem holds for any number of classes
  2. Yes, provided the classes are balanced
  3. No: with three or more classes, the optimal split need not be a prefix of any single ordering of the levels
  4. No, because Gini cannot be used with categorical features
Show the answer

C. No: with three or more classes, the optimal split need not be a prefix of any single ordering of the levels

The proof places each level at a point μℓ\mu_\ell on a line. With KK classes, each level is a point in the simplex ΔK\Delta_K, and an optimal two-group partition of points in two or more dimensions need not be cut out by any ordering along one line. Exact search is exponential in LL, so libraries order levels heuristically, for example along the first principal component of their class-probability vectors (Coppersmith, Hong & Hosking, 1999), or one-hot encode.

A many-level feature also carries a statistical cost. Even after ordering it offers L−1L-1 candidate splits, each fitted to the same labels, so a noise feature with many levels finds a good-looking split by chance more often than a binary one, and greedy growth favors it. Conditional inference trees (Hothorn, Hornik & Zeileis, 2006) remove this bias by separating the two questions: a permutation test chooses the variable, and only then is the best split point of that variable found.

Missing values and surrogate splits

CART handles a missing value of the split feature by asking a different feature. Let s∗s^* be the chosen split at node tt, sending fractions pL,pRp_L,p_R of the node's examples left and right. For every other feature j′j', find the split s~j′\tilde s_{j'} on j′j' that best reproduces where s∗s^* sends the examples (among those with both features observed). The majority rule, sending everything to the larger child, already errs on a fraction min⁡(pL,pR)\min(p_L,p_R); a surrogate is useful only if it does better:

λ(s~j′)=min⁡(pL,pR)−Pr⁡[s~j′ and s∗ disagree]min⁡(pL,pR)\lambda(\tilde s_{j'})=\frac{\min(p_L,p_R)-\Pr\big[\tilde s_{j'}\text{ and }s^*\text{ disagree}\big]}{\min(p_L,p_R)}
The relative reduction in routing error over the majority rule. Surrogates with λ>0\lambda>0 are kept and ranked; an example missing the primary feature follows the best surrogate whose feature it has, and the majority rule if it has none.

Surrogates also expose masking. A feature that is never chosen as a primary split, because a correlated feature is always slightly better, can be an excellent surrogate everywhere. CART's original variable importance therefore credits each feature with the impurity decrease of its best surrogate split at every node, not just its primary splits. Other systems handle missing values differently: C4.5 sends the example down every branch with weights proportional to the branch sizes and averages the predictions, and gradient-boosting libraries learn, at each split, the default direction that most reduces the loss for missing values. When missingness itself carries information, treating "missing" as a value of its own can beat any imputation (Missing Data).

ID3, C4.5 and the gain ratio

Quinlan's ID3 (1986) grew multiway trees on categorical attributes: a node splits on one attribute into one child per value, choosing the attribute with the largest information gain. Multiway splits expose a bias. An attribute with a distinct value for every example, such as a customer ID, splits the node into pure singletons and attains the largest possible gain, H(Y)H(Y), while being useless for prediction. Quinlan's remedy, described in the same paper and adopted by C4.5 (Quinlan, 1993), divides the gain by the entropy of the partition itself:

GR(A)=IG(A)SplitInfo(A),SplitInfo(A)=−∑vnvnlog⁡2nvn\mathrm{GR}(A)=\frac{\mathrm{IG}(A)}{\mathrm{SplitInfo}(A)},\qquad\mathrm{SplitInfo}(A)=-\sum_{v}\frac{n_v}{n}\log_2\frac{n_v}{n}
nvn_v examples take value vv of attribute AA. SplitInfo is large when AA shatters the node into many small pieces. Because the ratio explodes when SplitInfo is tiny, C4.5 maximizes it only among attributes whose gain is at least the average gain.
Predict before you look

A node has 16 examples, 8 of each class, so H(Y)=1H(Y)=1 bit. An ID attribute gives every example its own value. What is its gain ratio?

Show the answer

0.25

The gain is the full 1 bit, but SplitInfo =log⁡216=4=\log_216=4 bits, so GR =0.25=0.25. A binary attribute splitting the node into (7,1)(7,1) and (1,7)(1,7) has gain 1−H(7/8)≈0.4561-H(7/8)\approx0.456 bits and SplitInfo 1, so GR ≈0.456\approx0.456. Raw gain prefers the ID (1 against 0.456); the gain ratio prefers the genuine attribute.

AlgorithmSplitsSplit criterionSize controlMissing values
CART (Breiman et al., 1984)binaryGini or twoing; squared error for regressiongrow large, cost-complexity pruning chosen by cross-validationsurrogate splits
C4.5 (Quinlan, 1993)multiway on categorical, binary on numericgain ratioerror-based pruning with a pessimistic (upper confidence limit) error estimatefractional examples sent down every branch
CHAID (Kass, 1980)multiway, merging similar categorieschi-squared tests with Bonferroni adjustmentstop when no split is significanta separate category
Conditional inference trees (Hothorn et al., 2006)binarypermutation tests choose the variable, then the best split pointstop when no test is significantsurrogate splits
Four classic tree algorithms. Loh (2014) surveys these and many later ones.

Key takeaways

  • Concave impurities never increase under a split (Jensen); strictly concave ones such as Gini and entropy decrease unless the children's class proportions are equal, while misclassification error stays flat whenever both children keep the parent's majority.
  • Under a weak-learning assumption, greedy growth is a boosting algorithm whose progress depends on the criterion's curvature (Kearns & Mansour, 1999); misclassification error gives no guarantee.
  • Ft(α)=min⁡{R(t)+α, FtL+FtR}F_t(\alpha)=\min\{R(t)+\alpha,\,F_{t_L}+F_{t_R}\}, and the advantage of collapsing grows with α\alpha, so the smallest optimal subtrees are nested and finitely many; weakest-link pruning finds them, and cross-validation (often with the one-standard-error rule) picks one.
  • For regression and two-class problems, an optimal binary split of a categorical feature is a prefix of the levels sorted by mean response, so L−1L-1 splits suffice; with three or more classes no ordering is guaranteed.
  • Surrogate splits route missing values and reveal masked features; ID3's gain favors many-valued attributes, which C4.5's gain ratio IG/SplitInfo\mathrm{IG}/\mathrm{SplitInfo} corrects.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A split leaves both children with the parent's majority class, but makes one child much purer. Which impurity decrease is guaranteed to be exactly zero?

  1. Gini impurity
  2. Entropy
  3. Misclassification error
  4. All three
Show the answer

C. Misclassification error

With the majority class cc fixed, misclassification error is the affine function 1−p^(c)1-\hat p(c) of the proportions, and the parent's proportions are the weighted average of the children's, so the weighted error is unchanged. Gini and entropy are strictly concave, so they decrease whenever the children's proportions differ.

Question 2

Why are the smallest cost-complexity-optimal subtrees TαT_\alpha nested as α\alpha grows?

  1. Because the advantage of collapsing a node, Dt(α)D_t(\alpha), increases with α\alpha at rate at least 1, so once collapsing is optimal it stays optimal
  2. Because the training error of a subtree is never larger than that of the full tree
  3. Because Rα(T)R_\alpha(T) is a convex function of the tree
  4. Because weakest-link pruning removes exactly one leaf per step
Show the answer

A. Because the advantage of collapsing a node, Dt(α)D_t(\alpha), increases with α\alpha at rate at least 1, so once collapsing is optimal it stays optimal

Dt(α)=FtL(α)+FtR(α)−R(t)−αD_t(\alpha)=F_{t_L}(\alpha)+F_{t_R}(\alpha)-R(t)-\alpha, and each FF has slope at least 1 (every subtree has a leaf), so DtD_t has slope at least 1. Collapsing is optimal on an interval [at,∞)[a_t,\infty), and induction over the tree gives nesting.

Question 3

A regression tree considers a categorical feature with 8 levels whose mean responses are known. How many binary splits must it score to be sure of finding the optimal one?

  1. 28=2562^8=256
  2. 27−1=1272^7-1=127
  3. 88
  4. 77
Show the answer

D. 77

By the ordering theorem, some optimal split is a prefix of the levels sorted by mean response, and there are L−1=7L-1=7 nonempty proper prefixes. Exhaustive search would score all 27−1=1272^{7}-1=127 partitions.

Question 4

Why does C4.5 divide the information gain by the split information?

  1. To convert bits into a probability
  2. To penalize attributes that fragment the node into many small branches, which gain information by fragmentation alone
  3. To make the criterion strictly concave
  4. To make the tree invariant to monotone transformations of the features
Show the answer

B. To penalize attributes that fragment the node into many small branches, which gain information by fragmentation alone

An attribute with a distinct value per example reaches the maximum gain H(Y)H(Y) by splitting into singletons. Its split information, log⁡2\log_2 of the number of branches for equal-sized ones, is large, so the ratio exposes it. C4.5 restricts the choice to attributes with at least average gain, to avoid rewarding attributes with tiny split information.

Further reading

  • L. Breiman, J. H. Friedman, R. A. Olshen & C. J. Stone, Classification and Regression Trees (Wadsworth, 1984), chapters 3–5 and 10.
  • J. R. Quinlan, C4.5: Programs for Machine Learning (Morgan Kaufmann, 1993).
  • M. Kearns & Y. Mansour, "On the boosting ability of top-down decision tree learning algorithms," Journal of Computer and System Sciences 58(1), 109–128 (1999).
  • T. Hothorn, K. Hornik & A. Zeileis, "Unbiased recursive partitioning: a conditional inference framework," Journal of Computational and Graphical Statistics 15(3), 651–674 (2006).
  • W.-Y. Loh, "Fifty years of classification and regression trees," International Statistical Review 82(3), 329–348 (2014).