Decision Tree

Decision Tree

Split the data with simple yes-or-no questions.

Drag a split line, or pick a node in the tree and try another question. The tree asks yes-or-no questions until each group is one kind.

  • beach day
  • stay home
  • node in focus: drag its line
  • Gimpurity
yesnoyesnoTemp ≤ 19.0 °C15 daysWind ≤ 28.5 km/h212 days33 days

G=1−∑kpk 2\htmlClass{sym-line}{G} = 1 - \sum_{k} p_k^{\,2}

In focus: every day. 20 days: 11 beach day, 9 stay home → G = 1 − (0.55² + 0.45²) = 0.49

Split Temp ≤ 19.0 °C: gain = 0.49 − (5/20 × 0.00 + 15/20 × 0.39) = 0.20

All 3 leaves, weighted by size: G = 0.09

Right on its own data 19 of 20
Depth 2: 95% right

Beach days: 20 days, and the leaf each one lands in.
13329stay3stay
23410beach2beach
3204beach2beach
42228beach2beach
5333beach2beach
62016beach2beach
73518beach2beach
82315beach2beach
92731stay3stay
101410stay1stay
111525stay1stay
121533stay1stay
133134stay3stay
142615beach2beach
153021stay2beach ✗
161710stay1stay
172527beach2beach
181823stay1stay
19315beach2beach
20309beach2beach

Try it in Python

The same 20 days and depth as the Lab, in a few lines of Python. Plain Python grows the tree by hand, and keeps the questions you moved in the Lab.

What you are looking at

Each shape is a day at the seaside. Across the graph is the temperature; up the graph is the wind. Circles were good beach days and triangles were days to stay home. Good days are warm and not too windy, but a few days break the rule, as real days do.

The lines cut the chart into rectangles. Each rectangle is a leaf of the tree below it, and its tint shows the tree's answer for any day that lands there. Days the tree gets wrong have a dark ring. The numbers are made up, and Refresh Data shows another 20 days.

What each symbol means

node
A group of days, and the question that splits it in two. The top node holds every day.
leaf
A node with no question: it gives the answer.
pₖ
The share of days in a node that are of kind k.
G
Gini impurity: how mixed a node is. 0 when every day is the same kind; 0.5 when two kinds are split half and half.
gain
How much a question lowers the impurity.
d
The depth: how many questions in a row the tree may ask.

How a tree is grown

A decision tree is a list of yes-or-no questions, like "Is it 19 °C or colder?" Each answer sends a day left (yes) or right (no), until it reaches a leaf. The tree is grown from the top, one node at a time:

  1. Measure how mixed the node is.
    G=1−∑kpk 2\htmlClass{sym-line}{G} = 1 - \sum_{k} p_k^{\,2}
  2. Try every question: each feature, and every threshold halfway between two neighbouring values. Score each by how much purer its two halves are.
    gain=Gnode−(nyesn Gyes+nnon Gno)\text{gain} = \htmlClass{sym-line}{G}_{\text{node}} - \left( \tfrac{n_{\text{yes}}}{n}\, \htmlClass{sym-line}{G}_{\text{yes}} + \tfrac{n_{\text{no}}}{n}\, \htmlClass{sym-line}{G}_{\text{no}} \right)
  3. Keep the question with the highest gain, and grow each half the same way.
  4. Stop when a node holds only one kind, or the tree reaches its depth limit. A leaf answers with the most common kind among its days.

The tree is greedy: it picks the best question for each node on its own, without looking ahead. Move a split line and the gain drops, because the tree had already found the best place. The questions below it are then grown again from the new halves.

Choosing the depth

A shallow tree asks few questions, so it can only draw a few straight borders. Two questions are enough here: "Is it warm?" then "Is it calm?"

A deep tree keeps asking until almost every leaf is pure, so it gets nearly all of its own days right. But it does that by drawing small boxes around the odd days, and a new day that lands in one of those boxes gets a strange answer. The score Right on its own data always rises with depth, so it cannot tell you when to stop. In practice, the depth is chosen by testing the tree on days it has not seen.

The model in short

  1. Ask the yes-or-no question that makes the two halves purest.
  2. Repeat in each half.
  3. Stop at pure nodes or at the depth limit; each leaf answers with its most common kind.
  4. Predict by following the questions from the top down to a leaf.