← Explainer Library

Interactive Explainer

Decision Trees, Live

Click to drop training points; a CART decision tree grows axis-aligned splits live. Adjust depth, split criterion, minimum leaf size; watch the boundary go from too-simple to overfit-to-points. Then compare to logistic regression and a 25-tree random forest on the same dataset.

Prelude

Why trees are the boring-and-good default

For tabular data — financial features, sensor logs, survey responses, anything that arrives as columns in a CSV — a decision tree (or its ensembles) is almost always either the right answer or the right baseline. They handle missing values, mixed continuous + categorical features, and arbitrary monotone transformations of any feature, without normalisation. Their inductive bias is "constant predictions on axis-aligned regions," which lines up surprisingly well with how human-curated features actually carry information.

The catch: a single tree is too greedy and too jagged. The fix — bagging (random forests) and boosting (XGBoost) — is what makes the family dominate tabular competitions. This article focuses on the single-tree mechanism so the ensemble methods make sense.

Step 1

The greedy split rule

A CART decision tree builds itself top-down. At each node, it picks the (feature, threshold) pair that maximally decreases the impurity of the children. Standard impurities for classification:

For a candidate split, the impurity drop is $\Delta = I(\text{parent}) - \frac{N_L}{N}I(\text{left}) - \frac{N_R}{N}I(\text{right})$. Search over all features and all candidate thresholds, pick the maximum, recurse on each side. Stop when a node is pure, too small, or hits the depth limit.

Two consequences worth feeling: (1) splits are axis-aligned — the resulting decision regions are unions of rectangles. (2) the algorithm is greedy — a globally-better split sequence may exist but won't be found.

Step 1½

Live: information gain across thresholds

Drag the slider to pick a candidate threshold on a single feature. The two histograms show how the parent (top) splits into left/right children, and the curve below shows the impurity-decrease $\Delta$ over every possible threshold. The tree picks the argmax.

Top: data on the feature axis. Middle: impurity curve. Marker = current threshold.

Three things to notice:

Step 2

Build a tree, click by click

Left-click to add a class-A point (blue), right-click / shift-click for class-B (orange). The tree re-fits at every click. Push max depth up; watch the boundary become increasingly fragmented. Push min samples per leaf up; watch it smooth out.

Tree decision regions
Random forest (25 trees)
Logistic regression
Tree shape (BFS layout)
Step 3

What you should notice

Step 3½

Regression trees

The same recipe works for regression: replace Gini/entropy with squared-error impurity

At each leaf, predict the mean of the training $y$ values that fell there. The result is a piecewise-constant function of $x$ — terrible for smooth signals (use a GP), but excellent for tabular features where the right level set is itself piecewise-constant.

A subtlety: the split criterion for regression is equivalent to maximising the between-group variance at the split — i.e. an axis-aligned ANOVA. This explains why regression trees can find threshold-effects (e.g. "above 65 mph, fuel economy drops sharply") that linear models miss.

Step 3¾

Pruning & the cost-complexity path

Pre-pruning (depth limit, min-samples-per-leaf) is cheap but coarse. Post-pruning is principled: grow a full tree, then collapse subtrees that don't pay for themselves. CART's cost-complexity criterion:

As $\alpha$ increases from 0 to $\infty$, the optimal tree shrinks from the full tree to the root. The cost-complexity path is the sequence of trees you pass through; the recommended way to pick $\alpha$ is cross-validation on this path.

Step 3⅞

Feature importance & its pitfalls

Two ways to score feature importance from a fitted tree:

Step 4

What trees are good at — and what they're not

The slogan. A single decision tree is rarely the answer; an ensemble of trees often is. That ensemble (gradient-boosted) is what the XGBoost article covers next.
Step 5

CART, ID3, C4.5 — the family

Step 6

Reading list