Skip to content

Decision trees

A tree asks one yes-or-no question about one feature at a time, cutting the plane into boxes; each cut makes the boxes purer, and depth decides how far it chases noise.

Helps to know first
This topic

First, the picture

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.

mistakes on the 100 training dots
–
mistakes on 100 new dots
–
depth
–
leaves (boxes)
–
0.00 / 17.80 s
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 x2=−0.09x_2=-0.09: 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 pcp_c for the share of the box’s dots that are class cc; here it is a share of dots, not a probability. The Gini impurity of the box is

1−∑cpc2.1-\sum_cp_c^2.

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 mm dots into a left side of mleftm_\text{left} dots with impurity GleftG_\text{left} and a right side of mrightm_\text{right} with GrightG_\text{right}. Their impurity together is the weighted average

mleftm Gleft+mrightm Gright,\frac{m_\text{left}}{m}\,G_\text{left}+\frac{m_\text{right}}{m}\,G_\text{right},

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?

fall for cut A
–
fall for cut B
–
0.00 / 12.60 s
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 −∑cpclog⁡2pc-\sum_cp_c\log_2p_c 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 kk plays in Nearest neighbours, in the opposite direction: a small kk 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.

Two classes either side of a slanted line (dashed). Every question a tree asks cuts straight across or straight down, so its boxes can only follow the slant as a staircase; a deeper tree makes the steps finer, never slanted.
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 phealthy=0.6p_\text{healthy}=0.6 and pfaulty=0.4p_\text{faulty}=0.4, so its Gini impurity is

1−(0.62+0.42)=1−(0.36+0.16)=0.48.1-(0.6^2+0.4^2)=1-(0.36+0.16)=0.48.

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 1−((1/3)2+(2/3)2)=1−(1/9+4/9)=0.44441-\big((1/3)^2+(2/3)^2\big)=1-(1/9+4/9)=0.4444. Weighted: 0.4×0+0.6×0.4444=0.26670.4\times0+0.6\times0.4444=0.2667. The fall is 0.48−0.2667=0.21330.48-0.2667=0.2133.

Cut B, “temperature above 70 °C”. The two sides have 4 healthy and 1 faulty (Gini 1−(0.64+0.04)=0.321-(0.64+0.04)=0.32) and 2 healthy and 3 faulty (Gini 1−(0.16+0.36)=0.481-(0.16+0.36)=0.48). Weighted: 0.5×0.32+0.5×0.48=0.400.5\times0.32+0.5\times0.48=0.40. The fall is 0.48−0.40=0.080.48-0.40=0.08.

Cut A wins, by a wide margin: it makes one side pure.

With entropy. The box before any cut has −(0.6log⁡20.6+0.4log⁡20.4)=0.9710-(0.6\log_20.6+0.4\log_20.4)=0.9710 bits. After cut A: 0.6×0.9183=0.55100.6\times0.9183=0.5510 bits, a gain of 0.4200. After cut B: 0.5×0.7219+0.5×0.9710=0.84640.5\times0.7219+0.5\times0.9710=0.8464 bits, a gain of 0.1245. Entropy agrees: cut A.

Two edge cases. A box that is half and half has Gini 1−(0.25+0.25)=0.51-(0.25+0.25)=0.5. 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

QuantityFormulaNotes
Questionis one feature above a threshold?a cut across or down
Leafmajority class, or the mean for numbersone box of the plane
Gini impurity1−∑cpc21-\sum_cp_c^20 pure, 0.5 half and half (two classes)
SplitmleftmGleft+mrightmGright\frac{m_\text{left}}{m}G_\text{left}+\frac{m_\text{right}}{m}G_\text{right}choose the largest fall
Entropy−∑cpclog⁡2pc-\sum_cp_c\log_2p_cin bits; usually the same cuts
Thresholdshalfway between neighbouring valuesties: lower feature, then lower threshold
Greedybest single cut nownot necessarily the best whole tree
Depthtraining mistakes never risenew-data mistakes stop falling

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look