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 AClass BShaded 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 AClass BHollow rings = held-out test points (not used to build the tree).
Depth 3Train acc —Test acc —Gini —
3. The whole idea in four lines
Greedy & recursive. Look at every feature and every threshold, pick the single split
that reduces impurity the most, then repeat independently on the left and right children. Each
node never reconsiders earlier choices — it's locally greedy, not globally optimal.
Impurity criteria. "Best" is measured by an impurity score —
Gini (1 − Σ p²) or entropy (−Σ p·log p). Both are smallest when a node holds
a single class. A split is good when the weighted impurity of its two children is lower than the
parent's. (Regression trees swap impurity for variance.)
Depth & pruning fight overfitting. Left unchecked, a tree grows until every leaf is one
point — perfect on training data, useless on new data. Limiting depth, requiring a minimum number
of points per leaf, or pruning branches back keeps the model general.
A random forest averages many trees. One tree is twitchy. Train hundreds, each on a random
subset of the data and features, then average their votes — the noise cancels out and the
signal survives. That ensemble (a random forest) is far more accurate and stable than any
single tree.