← All animations

🌳 Decision Trees

A decision tree is just a flowchart of yes/no questions. Starting from all your data, it greedily picks the single split — "is feature X above some threshold?" — that best separates the classes, then repeats on each side. Each question carves the feature space into rectangular boxes; follow the questions to a leaf and you have a prediction. Scroll through to watch a tree grow, see impurity fall with every split, and learn why a tree that goes too deep simply memorises the noise.

1. Growing the tree — one greedy split at a time

On the left is a scatter of two classes; on the right, the tree of questions. Press Grow one split and the tree finds the axis-aligned cut that most reduces impurity (best separates the classes), draws it as a line, and shades the resulting decision regions. The regions on the scatter always match the leaves of the tree — both are the same model.

Class A Class B Shaded boxes = what the tree would predict there.
Depth 0 · 1 leaf

2. Impurity & overfitting — how deep is too deep?

At each split a tree measures Gini impurity — the chance you'd be wrong if you guessed a leaf's class at random (0 = perfectly pure, 0.5 = a 50/50 mix). Greedy splitting drives training impurity toward zero. But push the depth slider up and watch: a deep tree carves a tiny box around every noisy point — training accuracy hits 100%, yet accuracy on unseen test points falls. That gap is overfitting. The sweet spot is a shallow tree that captures the trend, not the noise.

Class A Class B Hollow rings = held-out test points (not used to build the tree).
Depth 3 Train acc — Test acc — Gini —

3. The whole idea in four lines