Foundations to Frontier
Tests
D. Classical Machine Learning › D6 Decision Trees

CART Classification Trees (Gini Impurity)

CART (Classification and Regression Trees; Breiman, Friedman, Olshen and Stone, 1984) grows strictly binary trees by greedily choosing, at every node, the split that most reduces an impurity measure, usually Gini impurity. Numeric features are cut at thresholds, and categorical features are split into two groups of categories, efficiently via an ordering trick. This page covers Gini impurity and its probabilistic meaning, the vectorised split search, categorical splits, the comparison with entropy and misclassification error, and why greedy axis-aligned trees struggle with XOR and diagonal boundaries. In the exercises you implement CART with fast split search and verify the category-ordering theorem; then you test whether the impurity criterion matters.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Always ask a yes/no question

CART trees only ask yes/no questions: “Is x₁ ≤ 0.3?” Each question cuts a box of feature space into two smaller boxes. CART chooses each question to make the two resulting groups as pure as possible, measured by Gini impurity: the chance that two randomly drawn examples from a group have different labels. Repeating this carves the space into rectangles, each predicting its majority class.

CART: greedy binary splits, level by level
|
leaves 4 · training accuracy 96.9%
Corner: with misclassification error, no first cut changes a majority (class 0 still wins on both sides), so the gain is 0 and the tree stays a single leaf; Gini and entropy reward the purer child and isolate the corner within two levels. XOR: no single cut helps any criterion much, so the first cut is close to luck. Diagonal: axis-aligned cuts need a staircase to follow a slanted boundary.
Why engineers care

CART is the tree inside scikit-learn, R’s rpart, random forests and every gradient-boosting library. Binary splits, Gini impurity and greedy search are the defaults you use daily, whether or not you notice them, and their quirks (axis-aligned staircases, greedy myopia) show up in every tree ensemble.

📝 Quick check (Level 0)
0/3 answered
  1. 1. CART splits are always…
  2. 2. Gini impurity of a pure node?
  3. 3. In the demo, misclassification error on 'corner' data…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Choosing a split by hand

Gini impurity: G = 1 − Σ p_c². Two classes in proportions p and 1 − p give G = 2p(1 − p).

Example. A node has 300 “ok” and 100 “fault”, so G = 1 − (0.75² + 0.25²) = 0.375.

Candidate split children weighted child Gini decrease
A (200 ok, 0 fault) and (100 ok, 100 fault) 0.5·0 + 0.5·0.5 = 0.25 0.125
B (150 ok, 50 fault) twice 0.375 (proportions unchanged) 0

CART chooses A. Misclassification error would score both as 0: the parent error is 0.25, A’s weighted error is 0.5·0 + 0.5·0.5 = 0.25, and B’s is 0.25, because neither changes a majority. This is why CART grows with Gini or entropy and reserves misclassification error for pruning.

Thresholds. For a numeric feature, sort the values and evaluate every midpoint. With cumulative class counts, all n − 1 candidates cost O(n) after sorting.

Categorical features. With two classes, sort the categories by their fraction of class 1. The best binary partition is one of the k − 1 “prefix” splits of that ordering (Breiman et al., 1984), not one of the 2^(k−1) − 1 possible subsets.

The steps in plain English:

  1. At a node, for each feature, find the best yes/no question and its impurity decrease.
  2. Split on the best question overall, if its decrease is positive and the children are large enough.
  3. Recurse on both children until a stopping rule applies (purity, depth, minimum leaf size).
  4. Predict with the leaf’s class proportions. In practice, prune afterwards (cost-complexity pruning).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Gini of a 50/50 node (two classes)?
  2. 2. Gini decrease of split A above?
  3. 3. Prefix splits to check for 6 categories and 2 classes?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

CART growing in detail

Impurity decrease: ΔI = I(parent) − (n_L/n)·I(L) − (n_R/n)·I(R).

Impurity measures for proportion p (two classes):

Measure formula at p = 0.5 strictly concave?
Gini 2p(1 − p) 0.5 yes
Entropy −p log₂ p − (1 − p) log₂(1 − p) 1 yes
Misclassification error min(p, 1 − p) 0.5 no (piecewise linear)

Strict concavity guarantees that every split which changes class proportions gets a positive decrease. Misclassification error, being piecewise linear, can give zero decrease to useful splits.

Gini’s meanings.

  • It is the expected error of a classifier that labels randomly according to the node’s proportions.
  • It is the variance of the class indicator, summed over classes, which links CART classification to CART regression.

Stopping and size control. Typical controls are max_depth, min_samples_leaf, min_impurity_decrease and max_leaf_nodes. CART’s own recipe is to grow large, then apply cost-complexity pruning with CV (see the pruning page).

Feature importance. The total weighted impurity decrease contributed by each feature (“mean decrease in impurity”). It is biased toward high-cardinality features; prefer permutation importance.

from sklearn.tree import DecisionTreeClassifier, export_text
clf = DecisionTreeClassifier(criterion="gini", min_samples_leaf=5, ccp_alpha=0.005).fit(X, y)
print(export_text(clf, feature_names=names))
🐍 Exercise 1: CART classification from scratch
Implement Gini impurity, a vectorised best numeric split, the category-ordering split for two classes (and an exhaustive version to check it), recursive CART fitting and probability prediction; verify the ordering theorem on random problems and that a full tree fits XOR while a stump cannot.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why isn't misclassification error used to grow trees?
  2. 2. Gini equals the expected error of…
  3. 3. Impurity-based feature importance is biased toward…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Greedy myopia. CART optimises one split at a time. For XOR, no single axis-aligned cut reduces impurity in expectation, so the first cut is driven by sampling noise, and later cuts may or may not recover the structure. Optimal trees, found by mixed-integer optimisation (Bertsimas and Dunn, 2017), avoid this at a large computational cost.

Axis-aligned bias. A diagonal boundary needs a staircase of many cuts, a motivation for oblique trees (see the oblique-trees page) and for ensembles.

Consistency. Partitioning estimates such as CART are consistent if cells shrink and each contains many points as n → ∞ (Stone, 1977; Devroye, Györfi and Lugosi, 1996). Greedy data-dependent splits complicate the theory, but versions with suitable stopping are consistent.

Gini versus entropy. Their curves have very similar shapes: entropy is about 2 to 2.6 times Gini for p between 0.1 and 0.5. Raileanu and Stoffel (2004) found they disagree on only about 2% of split choices. Exercise 2 agrees: identical root splits in 80–90% of runs, and the same tuned accuracy to within 0.01 on three of four problems.

Interview questions.

  • Gini or entropy? It rarely matters. Gini is slightly cheaper (no logarithms). Tune depth or pruning instead.
  • Why can a tree fail on XOR even with enough data? Greedy split selection sees no gain from any single first cut. Look-ahead, ensembles or feature engineering (x₁·x₂) fix it.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Gini and entropy choose different splits in about…
  2. 2. Trees approximate a diagonal boundary with…
  3. 3. Optimal decision trees can be found with…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Does the impurity criterion matter?

History.

  • Morgan and Sonquist’s AID (1963) introduced binary regression trees.
  • Breiman, Friedman, Olshen and Stone’s Classification and Regression Trees (1984) gave Gini, surrogate splits, cost-complexity pruning and a sound statistical treatment.
  • CART became the basis of bagging (1996), random forests (2001) and gradient boosting (2001).
  • Optimal trees (2017 onward) revisited CART’s greedy search.

Exercise 1.

  • Gini(0, 0, 1, 1) = 0.5, and the best cut is x ≤ 3.5.
  • The category-ordering trick matched exhaustive subset search on 20 random problems.
  • A full CART tree fits XOR (test accuracy 0.994 with 200 training points), while a single split reaches only 0.496.

Exercise 2. Setup: CART grown with each criterion and depth tuned on a validation set, 10 repetitions. The table shows test accuracy.

Problem Gini Entropy Misclassification
overlapping blobs (5-D) 0.642 0.641 0.651
3-class rules + noise 0.930 0.928 0.929
XOR (2 of 6 features) 0.763 0.683 0.783
“corner” (needs a majority-preserving first cut) 0.942 0.943 0.842
  • Gini and entropy are interchangeable. They chose the same root split in 80–90% of runs and tuned to within 0.01 on blobs, rules and corner.
  • Misclassification error failed exactly where theory says it should. On “corner”, the first useful cut leaves class 0 the majority on both sides, so its decrease is 0 and growth stalls (2.2 leaves on average, against 4).
  • It was not worse elsewhere. Its reluctance to split acted as regularisation on noisy blobs.
  • XOR is a greedy trap. First cuts are close to random under any criterion, so the 0.68–0.78 spread there reflects luck in the first split, not a real ranking.
🐍 Exercise 2 (research): Gini vs entropy vs misclassification error
On four synthetic problems, grow CART trees with each impurity criterion, tune depth on a validation set, and report test accuracy, leaves and how often Gini and entropy choose the same root split (10 repetitions).

CART in AI, robotics and aerospace

  • The base learner of tree ensembles used for telemetry classification, anomaly triage and predictive maintenance.
  • Interpretable go/no-go rules for launch commit criteria and test acceptance.
  • Fast embedded inference: a depth-d tree costs d comparisons.
  • Policy distillation: compact trees that imitate neural controllers for verification.

References

  1. L. Breiman, J. H. Friedman, R. A. Olshen, C. J. Stone (1984). Classification and Regression Trees. Wadsworth.
  2. L. E. Raileanu, K. Stoffel (2004). “Theoretical comparison between the Gini index and information gain criteria.” Annals of Mathematics and Artificial Intelligence 41.
  3. D. Bertsimas, J. Dunn (2017). “Optimal classification trees.” Machine Learning 106.
  4. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 9.2. Springer.
  5. J. N. Morgan, J. A. Sonquist (1963). “Problems in the analysis of survey data, and a proposal.” JASA 58.