Decision-tree pruning: worked solutions

Problem

We are comparing two classification trees. Tree A has three terminal nodes (leaves); Tree B is obtained by pruning Tree A and has two terminal nodes.

We choose the tree that minimizes the weighted-average Gini impurity plus a complexity penalty:

\[ T_{\text{best}} = \underset{T \subseteq T_0}{\arg\min} \left( \sum_{m=1}^{|T|} \frac{N_m}{N} \sum_{k=1}^{K} \hat{p}_{mk}(1-\hat{p}_{mk}) + \alpha |T| \right) \]

Here:

For each leaf, its Gini impurity is

\[ G_m = \sum_{k=1}^{3}\hat p_{mk}(1-\hat p_{mk}). \]

We’ll compute the Gini impurity of every leaf, weight it by \(N_m/N\), sum the weighted contributions, and then add \(\alpha|T|\).

1. Tree A: three leaves

Leaf (predicted class) Red Green Blue \(N_m\)
Leaf 1 (left) 3 2 9 14
Leaf 2 (middle) 15 8 3 26
Leaf 3 (right) 22 30 28 80
Total 40 40 40 120

Leaf 1: predicts blue (\(N_1=14\))

\[ \begin{aligned} G_1 &= \frac{3}{14}\left(1-\frac{3}{14}\right) + \frac{2}{14}\left(1-\frac{2}{14}\right) + \frac{9}{14}\left(1-\frac{9}{14}\right)\\ &= \frac{3}{14}\cdot\frac{11}{14} + \frac{2}{14}\cdot\frac{12}{14} + \frac{9}{14}\cdot\frac{5}{14}\\ &= \frac{33+24+45}{196} = \frac{102}{196} \approx 0.520408. \end{aligned} \]

Weighted contribution:

\[ \frac{14}{120}G_1 =\frac{14}{120}(0.520408\ldots) \approx \boxed{0.060714}. \]

Leaf 2: predicts red (\(N_2=26\))

\[ \begin{aligned} G_2 &= \frac{15}{26}\left(1-\frac{15}{26}\right) + \frac{8}{26}\left(1-\frac{8}{26}\right) + \frac{3}{26}\left(1-\frac{3}{26}\right)\\ &= \frac{15}{26}\cdot\frac{11}{26} + \frac{8}{26}\cdot\frac{18}{26} + \frac{3}{26}\cdot\frac{23}{26}\\ &= \frac{165+144+69}{676} = \frac{378}{676} \approx 0.559172. \end{aligned} \]

Weighted contribution:

\[ \frac{26}{120}G_2 =\frac{26}{120}(0.559172\ldots) \approx \boxed{0.121154}. \]

Leaf 3: predicts green (\(N_3=80\))

\[ \begin{aligned} G_3 &= \frac{22}{80}\left(1-\frac{22}{80}\right) + \frac{30}{80}\left(1-\frac{30}{80}\right) + \frac{28}{80}\left(1-\frac{28}{80}\right)\\ &= \frac{22}{80}\cdot\frac{58}{80} + \frac{30}{80}\cdot\frac{50}{80} + \frac{28}{80}\cdot\frac{52}{80}\\ &= \frac{1276+1500+1456}{6400} = \frac{4232}{6400} = 0.661250. \end{aligned} \]

Weighted contribution:

\[ \frac{80}{120}G_3 =\frac{80}{120}(0.661250) \approx \boxed{0.440833}. \]

Tree A: total score

Weighted-average Gini impurity (using unrounded contributions):

\[ \begin{aligned} \frac{14}{120}G_1 +\frac{26}{120}G_2 +\frac{80}{120}G_3 &\approx 0.060714+0.121154+0.440833\\ &\approx \boxed{0.622701}. \end{aligned} \]

Since Tree A has three leaves, its penalized score is

\[ \boxed{\operatorname{Score}(A) \approx 0.622701+3\alpha.} \]

2. Tree B: two leaves (pruned)

Pruning merges Tree A’s blue-predicting and red-predicting leaves into one leaf with \(14+26=40\) observations. The 80-observation green-predicting leaf is unchanged.

Leaf (predicted class) Red Green Blue \(N_m\)
Leaf 1 (left) 18 10 12 40
Leaf 2 (right) 22 30 28 80
Total 40 40 40 120

Leaf 1: predicts red (\(N_1=40\))

\[ \begin{aligned} G_1 &= \frac{18}{40}\left(1-\frac{18}{40}\right) + \frac{10}{40}\left(1-\frac{10}{40}\right) + \frac{12}{40}\left(1-\frac{12}{40}\right)\\ &= \frac{18}{40}\cdot\frac{22}{40} + \frac{10}{40}\cdot\frac{30}{40} + \frac{12}{40}\cdot\frac{28}{40}\\ &= \frac{396+300+336}{1600} = \frac{1032}{1600} = 0.645000. \end{aligned} \]

Weighted contribution:

\[ \frac{40}{120}G_1 =\frac{40}{120}(0.645000) =\boxed{0.215000}. \]

Leaf 2: predicts green (\(N_2=80\))

This is identical to Tree A’s green-predicting leaf:

\[ \begin{aligned} G_2 &= \frac{22}{80}\left(1-\frac{22}{80}\right) + \frac{30}{80}\left(1-\frac{30}{80}\right) + \frac{28}{80}\left(1-\frac{28}{80}\right)\\ &= 0.661250. \end{aligned} \]

Weighted contribution:

\[ \frac{80}{120}G_2 =\frac{80}{120}(0.661250) \approx\boxed{0.440833}. \]

Tree B: total score

Weighted-average Gini impurity:

\[ \begin{aligned} \frac{40}{120}G_1+\frac{80}{120}G_2 &=0.215000+0.440833\ldots\\ &\approx \boxed{0.655833}. \end{aligned} \]

Since Tree B has two leaves, its penalized score is

\[ \boxed{\operatorname{Score}(B) \approx 0.655833+2\alpha.} \]

3. Final comparison

Lower is better, because the criterion is minimized.

\(\alpha\) Tree A (3 leaves) Tree B (2 leaves) Chosen tree
0.01 0.652701 0.675833 Tree A (3 leaves)
0.05 0.772701 0.755833 Tree B (2 leaves)

So:

4. Optional: what’s the critical value of \(\alpha\)?

In other words, “at what value of \(\alpha\) would we be exactly indifferent between trees A and B?”

To determine this, we set the two penalized scores equal:

\[ 0.622701465\ldots+3\alpha =0.655833333\ldots+2\alpha. \]

Solving,

\[ \boxed{\alpha\approx0.033132.} \]

Thus, Tree A is preferred below this threshold; Tree B is preferred above it; and the trees tie at the exact threshold (approximately \(0.033132\)).