Decision Trees
Recursive partitioning, entropy, information gain — and the exact 4-row worked example from the practice sheet.
This is the highest-value deck in the course for the written paper. The practice sheet's decision-tree question is lifted verbatim from slide 27 of this deck, and the entropy question is a direct application of slide 20.
What a decision tree is
> A decision tree is a hierarchical model for supervised learning in which local regions are identified by a sequence of recursive partitioning in a small number of steps.
- Decision nodes — a test function (a condition)
- Branches — discrete outcomes of that test
- Leaf nodes — the output: a class label for classification, a real value for regression

Three canonical examples appear on consecutive slides: play outside (weather → humidity/wind), Iris species (sepal length → sepal width), and the XOR truth table as a tree — each row of the table is one root-to-leaf path.



The only design question
"How to select the attribute?" — the answer is maximise information gain, which comes from information theory.

Entropy
> Entropy / impurity measures the level of impurity (how mixed-up or uncertain) a subset of examples is.
$$\boxed{\;Entropy = \sum_{i=1}^{c} -p_i \log_2(p_i)\;}$$
where $p_i$ is the probability of class $i$, estimated as the proportion of class $i$ in the set.

The slide's worked instance: a subset with $\tfrac15$ setosa, $\tfrac15$ virginica, $\tfrac35$ versicolor.
$$\log_2\!\left(\tfrac15\right) = -2.3, \qquad \log_2\!\left(\tfrac35\right) = -0.7$$ $$H = -\tfrac15(-2.3) - \tfrac35(-0.7) - \tfrac15(-2.3) = 0.88 \text{ bits}$$
Sanity checks that make the formula memorable:
| Subset | $H$ |
|---|---|
| all one class (pure) | $0$ |
| two classes, 50/50 | $1$ |
| three classes, equal thirds | $1.585 = \log_2 3$ |
| $c$ classes, uniform | $\log_2 c$ (maximum) |
Base 2 gives bits, which is why the slide writes $\log_2$. Any base works for comparing splits because it is a constant factor — but if you switch base mid-answer you lose the units mark.
Information gain
$$IG = E(\text{parent}) - E_{\text{avg}}(\text{children}), \qquad E_{\text{avg}} = \sum_{v}\frac{|S_v|}{|S|}\,E(S_v)$$
That weighted average is the whole trick: a split is good if the children are purer and you did not achieve that purity by shattering the data into tiny subsets.
Forgetting the weights $\frac{|S_v|}{|S|}$ is the single most common error. A split that isolates one pure sample and leaves ninety-nine mixed ones has $E_{\text{avg}}$ dominated by the big child, not the pure one.
The algorithm
- (Root) Select the attribute with the highest information gain on the full training set.
- (Child) Construct a branch for each value of the root; each child gets the subset of vectors having that value.
- Recurse on each child.
Stop when a node is pure, or no attributes remain, or a stopping criterion (max depth, min samples) is met.

Worked example — the exact practice-sheet question

| $x_1$ | $x_2$ | $x_3$ | $y$ |
|---|---|---|---|
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 2 |
| 1 | 1 | 1 | 2 |
| 1 | 1 | 0 | 1 |
Step 0 — parent entropy. $y = \{1,2,2,1\}$: two of each class.
$$H(S) = -\tfrac12\log_2\tfrac12 - \tfrac12\log_2\tfrac12 = 1 \text{ bit}$$
Step 1 — information gain for each feature.
$x_1$: value 0 → $\{y=1\}$ pure, $H=0$, $|S_v|=1$. Value 1 → $\{2,2,1\}$, $H=-\tfrac23\log_2\tfrac23-\tfrac13\log_2\tfrac13 = 0.918$. $|S_v|=3$.
$$E_{\text{avg}} = \tfrac14(0) + \tfrac34(0.918) = 0.689, \qquad IG = 1 - 0.689 = \mathbf{0.311}$$
$x_2$: value 1 → $\{1,2,1\}$, $H=0.918$, $|S_v|=3$. Value 0 → $\{2\}$ pure, $H=0$, $|S_v|=1$.
$$E_{\text{avg}} = \tfrac34(0.918) + \tfrac14(0) = 0.689, \qquad IG = \mathbf{0.311}$$
$x_3$: value 0 → $\{1,2,1\}$, $H=0.918$, $|S_v|=3$. Value 1 → $\{2\}$ pure, $H=0$, $|S_v|=1$.
$$E_{\text{avg}} = \tfrac34(0.918) = 0.689, \qquad IG = \mathbf{0.311}$$
All three features tie at $IG = 0.311$. That is not a mistake — each feature separates exactly one row from the other three in the same 1-vs-3 pattern. Break the tie arbitrarily.
Step 2 — pick $x_2$ as root and recurse.
- $x_2 = 0$ → row 2 only → class 2. Leaf.
- $x_2 = 1$ → rows 1, 3, 4, i.e. $y=\{1,2,1\}$, $H = 0.918$.
Within that child, test $x_3$:
- $x_3 = 1$ → row 3 → class 2. Leaf.
- $x_3 = 0$ → rows 1 and 4 → both class 1. Leaf.
Final tree
x2
/ \
=0 =1
| |
y=2 x3
/ \
=1 =0
| |
y=2 y=1
Two splits, four leaves, zero training error. Verify row by row: (0,1,0)→$x_2{=}1,x_3{=}0$→1 ✓; (1,0,0)→$x_2{=}0$→2 ✓; (1,1,1)→$x_2{=}1,x_3{=}1$→2 ✓; (1,1,0)→$x_2{=}1,x_3{=}0$→1 ✓.
How to write this up for marks. Show the parent entropy first, then a table of $IG$ per feature with the weighted averages visible, then state the root. Marks are almost always allocated as: parent entropy (1), correct $E_{\text{avg}}$ with weights (2), correct $IG$ per feature (2), correct recursion to pure leaves (2). If you only draw the tree you lose the first five.
Decision boundary
Because each split is an axis-aligned threshold, a tree produces rectangular regions — a staircase approximation to any boundary, which is why deep trees can fit arbitrarily complex data (and overfit).

Why trees overfit and three fixes. A fully grown tree can always drive training error to zero by isolating every point; there are no constraints on how many rectangles it carves. Remedies that appear across the course: pre-pruning via max_depth / min_samples_leaf, post-pruning, and — most importantly for the ensemble section — bagging/random forests, which average many deep trees so the variance cancels.






















