Decision tree: how splits, impurity and pruning work
A decision tree predicts an outcome by asking a sequence of yes/no questions about the inputs, such as "is the seal temperature at or below 167 °C?". Each answer leads either to another question or to a leaf, which gives the prediction: the most common class for a pass/fail outcome, or the average for a measured one. Trees are easy to read and find interactions and thresholds without being told where to look. A single tree is also unstable, which is why a random forest and gradient boosting combine many of them.
When to use it
- You have process records with many candidate inputs and want readable rules, such as "leaks happen when the temperature is low or the dwell is short".
- The outcome is a class (pass/fail, defect type) or a measurement, and the inputs mix numbers and categories.
- You suspect thresholds and interactions that a straight-line model would miss.
- You want a first look at which inputs matter before planning a designed experiment.
It is a poor choice when you need smooth predictions, precise effect sizes or results from a few dozen rows that must hold up exactly.
How it works
Choosing a split. For every input and every threshold between two consecutive observed values, the tree divides the rows into two groups and measures how mixed each group still is. For a class outcome the usual measure is the Gini impurity; for a measured outcome it is the sum of squared errors around each group's mean.
Gini of a node = 1 − Σ pₖ² (two classes: 2p(1 − p))
Impurity after a split = (n_L · G_L + n_R · G_R) / nThe split with the lowest impurity after splitting wins. A pure node (all one class) has Gini 0; a 50/50 node has Gini 0.5.
Growing. The same search repeats inside each new group until a stopping rule applies: a maximum depth, a minimum number of rows per leaf, or no split that improves the impurity enough.
Pruning. A tree grown until every leaf is pure fits the training data perfectly and new data badly. Cost-complexity pruning trades error against size:
Cost(T) = R(T) + α · (number of leaves)R(T) is the training error. For each α, the best tree is the one with the lowest cost; α = 0 keeps the full tree and a large α leaves only the root. α is chosen by cross-validation, as in automated model comparison.
Example
A packaging line makes heat-sealed pouches. Records for 20 pouches give the seal temperature, the dwell time and the result of a leak test.
| Temp (°C) | Dwell (s) | Result | Temp (°C) | Dwell (s) | Result |
|---|---|---|---|---|---|
| 158 | 0.8 | Leak | 175 | 0.5 | Leak |
| 161 | 0.6 | Leak | 176 | 0.9 | Pass |
| 163 | 0.9 | Leak | 178 | 0.6 | Pass |
| 164 | 0.7 | Leak | 179 | 0.4 | Leak |
| 166 | 0.8 | Leak | 181 | 0.5 | Pass |
| 168 | 0.9 | Pass | 182 | 0.7 | Pass |
| 169 | 0.6 | Leak | 184 | 0.5 | Leak |
| 171 | 0.8 | Pass | 185 | 0.9 | Pass |
| 172 | 0.4 | Leak | 187 | 0.6 | Pass |
| 174 | 0.7 | Pass | 189 | 0.8 | Pass |
Root. 10 leaks in 20 pouches: Gini = 2 × 0.5 × 0.5 = 0.5.
First split. Scanning all 19 temperature thresholds, the best is 167 °C: the 5 pouches below it all leak (Gini 0), and the 15 above it have 5 leaks (Gini 2 × ⅓ × ⅔ = 0.444). The impurity after the split is 15/20 × 0.444 = 0.333. The best dwell split at this stage, 0.55 s, only reaches 0.44, so temperature goes first.
Second split. Inside the 15 pouches above 167 °C, the best split is dwell at 0.55 s: 5 pouches with 4 leaks and 10 with 1 leak, an impurity of 0.227 against 0.444 before. The best temperature split in this branch only reaches 0.389.
The tree reads:
- Temperature ≤ 167 °C → leak (5 of 5 leaked)
- Temperature > 167 °C and dwell ≤ 0.55 s → leak (4 of 5)
- Temperature > 167 °C and dwell > 0.55 s → pass (9 of 10 passed)
It misclassifies 2 of the 20 pouches: one that passed at 181 °C and 0.5 s, and one that leaked at 169 °C and 0.6 s.
Pruning. The training error is 10/20 = 0.50 for the root alone, 5/20 = 0.25 for the one-split tree and 2/20 = 0.10 for the three-leaf tree. The three-leaf tree has the lowest cost while α < (0.25 − 0.10)/1 = 0.15; above that the one-split tree wins, and above 0.25 the root alone. With real data, choose α by cross-validation rather than by eye.
How to read the results
- Follow a path from the top. Each leaf is a rule: here, keep the temperature above 167 °C and the dwell above 0.55 s.
- The tree found an interaction. Dwell matters only once the temperature is high enough. A tree shows this without anyone adding an interaction term.
- Look at the counts in each leaf. "4 of 5" is much weaker evidence than "9 of 10", and both are small.
- Thresholds are midpoints. 167 °C means "somewhere between 166 and 168 °C, where we have no data".
Common mistakes
- Growing a deep tree with no pruning. With 20 rows a tree can classify every pouch correctly and still predict new pouches poorly.
- Reading the tree as cause and effect. With process records, an input that tracks the real cause (such as line speed changing with temperature) can take its place in the tree. Confirm with a designed experiment.
- Judging the tree by training accuracy. Use cross-validation or a holdout set.
- Using accuracy with rare defects. At a 1% defect rate, a tree that predicts "pass" for everything is 99% accurate and useless. Look at how many defects it catches.
Frequently asked questions
Gini or entropy?
They usually give the same or very similar trees. Entropy is −Σ pₖ log pₖ. Both reward splits that make nodes purer even when the majority class does not change, which plain misclassification rate does not.
How is a regression tree different?
Only in the impurity and the leaf value: it splits to reduce the sum of squared errors, and each leaf predicts the mean of its rows.
Why did my tree change when I added a few rows?
Trees are unstable: if two splits are almost tied, a few rows decide which one wins, and everything below it changes. Averaging many trees fixes this, at the cost of readability.
Can a tree use categorical inputs?
Yes. A split sends some categories left and the rest right, such as "supplier A or C" against "supplier B".