A game of yes-or-no questions can sort dots too. Below, a tree asks them one at a time. Before it plays: will a deeper tree make fewer mistakes on new dots?
Splitting the plane into boxes
100 labelled dots in two crescents; a tree asks one yes-or-no question about one feature at a time.
100 training dots in two crescents: 50 of class A, 50 of class B. A tree will sort them by yes-or-no questions about one feature at a time.
Describe this picture
Two panels. On the left, the plane of Nearest neighbours: 100 training dots in two crescents that overlap at their tips, 50 round dots of class A and 50 squares of class B, feature 1 across and feature 2 up. On the right, a tree diagram. In the clip cuts appear one at a time, each a straight line across or down that splits one box of the plane in two, and each box is tinted with the class most of its dots belong to. The tree grows beside the plane in step, a question at each fork and a box at each leaf: to depth 1, then 2, then 3, then straight to 6. The captions: at depth 1 “one question already sorts most of the dots”; at depth 2 “two more questions trim the ends”; at depth 3 “the next questions carve off boxes that hold one class only”; at depth 6 “the tree now cuts strips round single dots”. Readouts give the mistakes on the 100 training dots and on 100 new dots from the same crescents, never drawn, the depth and the number of leaves, or boxes. After the clip a slider, “Maximum depth”, runs from 1 to 9 and starts at 6.
Reading a tree
A decision tree is a list of questions arranged so that each answer leads to the next question. Each question asks about one feature: “is feature 2 above this value?”. Start at the top, the root, answer, follow the branch, and keep going until no question is left. That last stop is a leaf, and it gives the prediction: the class most of the training dots that reach it belong to (a tie goes to class A), or, when the labels are numbers, their average.
On the plane, each question is a straight cut across or down, because it looks at one feature only. A tree of questions cuts the plane into boxes, one box per leaf. The depth of a tree is the largest number of questions asked on the way to any leaf.
The first cut in the clip runs across the plane, at about : above it mostly class A, below it mostly class B. That one question leaves only 12 of the 100 dots on the wrong side. The next two cut down, near the left and right edges, and trim the crescents’ outer tips, where each crescent reaches past the other. The cuts at depth 3 change no box’s class: they only carve off boxes that hold one class, so the mistakes stay at 8.
Choosing a cut
Which question first? The tree tries every one: each feature, and each threshold halfway between two neighbouring values of that feature among the dots in the box. It keeps the cut that makes the two new boxes purest. That needs a measure of how mixed a box is.
A box whose dots are all one class is pure. Write for the share of the box’s dots that are class ; here it is a share of dots, not a probability. The Gini impurity of the box is
It is 0 for a pure box, and for two classes it is largest, 0.5, when the box is half and half. A cut splits a box of dots into a left side of dots with impurity and a right side of with . Their impurity together is the weighted average
and the tree chooses the cut whose weighted impurity falls furthest below the box’s own. If two cuts fall by exactly the same amount, the one on the lower-numbered feature wins, then the one with the lower threshold.
Two questions, one choice
The worked example: ten pumps, two candidate cuts, and the fall in Gini impurity each one gives.
Ten pumps: 6 healthy, 4 faulty, a Gini impurity of 0.48. Which question splits them better?
Describe this picture
The ten pumps of the worked example below, 6 healthy and 4 faulty, placed by their vibration and temperature, with a Gini impurity of 0.48. Two candidate cuts draw side by side: cut A asks “vibration above 4 mm/s” and cut B “temperature above 70 °C”. The counts and Gini impurity of each cut’s two boxes appear: cut A leaves a pure box and a mixed one, 0.4444; cut B leaves two mixed boxes, 0.32 and 0.48. Then both weighted results and their falls appear together: 0.2667, a fall of 0.2133, for cut A, and 0.40, a fall of 0.08, for cut B. Only then does cut B fade and cut A stay as the chosen question. Readouts give the fall for each cut. There is a play button and no other control.
A second measure, the entropy in bits, is used in the same way and usually picks the same cuts.
Growing and stopping
The tree is greedy: it picks the best single cut now and never goes back. Trying every whole tree at once would mean far too many to compare, so the tree it finds is not necessarily the best possible.
Left alone, it keeps cutting until every box is pure. Then it makes no mistakes on its training dots, but the last cuts are thin strips round single stray dots, which is noise, not shape. So a tree is stopped early: at a maximum depth, or when a box holds too few dots to split.
Now the answer to the question. On the training dots, the mistakes never rise as the tree grows: 12 at depth 1, 8 at depths 2 and 3, and on down to 0 at depth 9, where every box is pure. On new dots they fall from 22 to 9 at depth 2, and never improve after that: they are lowest at depths 2 to 4, 9 of 100, and from depth 5 on they come and go between 10 and 16 as the strips chase the training dots’ noise.
Watch out
A deeper tree is not a better tree. Past a few levels it fits the training dots ever more closely while doing no better on new ones.
That is the trade of Bias and variance of a model again: a shallow tree is too simple to follow the crescents, and a deep one changes with every stray dot. Depth plays the part that plays in Nearest neighbours, in the opposite direction: a small and a deep tree both follow the noise.
What trees cannot draw
Every cut is across or down, so a tree can only draw boundaries made of horizontal and vertical pieces.
a tree of depth 3
grown until every box is pure
Describe this picture
Two plots of the same dots: round class A dots above a dashed slanted line, square class B dots below it. Each plot is tiled by a tree’s boxes, shaded by class. On the left, a tree of depth 3: a few large boxes in a coarse staircase along the line. On the right, a tree grown until every box is pure: many smaller boxes in a finer staircase. The caption ends “a deeper tree makes the steps finer, never slanted”. There is no control.
A slanted boundary costs a tree many cuts, and it still only approximates it, where Logistic regression draws it with one line. Each kind of model has shapes it draws easily and shapes it struggles with.
Worked example
Ten pumps, made-up data: 6 healthy and 4 faulty. Before any cut the box has and , so its Gini impurity is
Cut A, “vibration above 4 mm/s”. The “no” side has 4 healthy and 0 faulty, Gini 0. The “yes” side has 2 healthy and 4 faulty, Gini . Weighted: . The fall is .
Cut B, “temperature above 70 °C”. The two sides have 4 healthy and 1 faulty (Gini ) and 2 healthy and 3 faulty (Gini ). Weighted: . The fall is .
Cut A wins, by a wide margin: it makes one side pure.
With entropy. The box before any cut has bits. After cut A: bits, a gain of 0.4200. After cut B: bits, a gain of 0.1245. Entropy agrees: cut A.
Two edge cases. A box that is half and half has Gini . A pure box has Gini 0.
Where you’ll meet this
Decisions that must be explained. A tree can be read aloud, question by question, so it suits rules that must be justified to the people they affect, such as credit decisions or triage in a hospital.
Fault-finding. A checklist that asks one reading at a time to locate a machine’s fault is a tree, and a fitted tree can suggest which readings to ask first.
Forests and boosting. Many trees together make some of the most used models for tables of business and sensor data. Random forests average many trees grown on resampled data, and Boosting adds small trees one at a time, each correcting the last.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Question | is one feature above a threshold? | a cut across or down |
| Leaf | majority class, or the mean for numbers | one box of the plane |
| Gini impurity | 0 pure, 0.5 half and half (two classes) | |
| Split | choose the largest fall | |
| Entropy | in bits; usually the same cuts | |
| Thresholds | halfway between neighbouring values | ties: lower feature, then lower threshold |
| Greedy | best single cut now | not necessarily the best whole tree |
| Depth | training mistakes never rise | new-data mistakes stop falling |