Exercises — Gini impurity
A reminder of the two building blocks, in plain words:
Level 1 — Recognition
Exercise 1.1
A node holds 20 items, all of the same class. What is its Gini impurity?
Recall Solution 1.1
One class means and every other . Answer: . A pure box — you can never lose the guessing game, so the mistake probability is zero.
Exercise 1.2
A 2-class node is a perfect 50/50 mix. Without computing, state whether its Gini is at its minimum or maximum, then give the number.
Recall Solution 1.2
For classes the maximum possible Gini is , reached exactly at 50/50. Answer: maximum, .
Exercise 1.3
Four nodes have proportions , , , . Rank them from purest to most impure by inspection (no full calculation needed).
Recall Solution 1.3
Purity is highest when one class dominates. As the mix approaches 50/50, impurity rises. Order (purest → most impure): . Check with numbers: . The figure below plots the whole 2-class Gini curve and drops these four nodes onto it. Notice how quickly the curve climbs away from the pure ends: moving from to jumps impurity from to — nearly the maximum — even though only slid from to . The lesson to read off the curve: impurity is insensitive near the pure ends (flat) and very sensitive in the middle (steep), which is why a split that pushes a node even slightly past the halfway point barely helps, while one that nudges a near-pure node the rest of the way barely helps either — the payoff lives in the steep middle.

Level 2 — Application
Exercise 2.1
A node has 3 red, 1 blue, 1 green (5 items total). Compute its Gini impurity.
Recall Solution 2.1
Proportions: , . Answer: .
Exercise 2.2
Parent node: 6 positive, 6 negative (12 items). A split produces Left = (5+, 1−) with 6 items, Right = (1+, 5−) with 6 items. Compute , each child's Gini, , and the Gini gain.
Recall Solution 2.2
Parent : Left : Right — same numbers by symmetry: Weighted split (each child holds of the data): Gini gain: Answer: , gain .
Exercise 2.3
A node of 8 items is 100% one class, so . You split it anyway into two children of 4 and 4, both still pure. What is the Gini gain? Why does the tree gain nothing here?
Recall Solution 2.3
Both children are pure: , so . Answer: gain . There is no impurity to remove from an already-pure node, so no split can improve it. This is exactly why trees stop splitting pure leaves — a degenerate but crucial case.
Level 3 — Analysis
Exercise 3.1
Parent: 10 items, (7+, 3−). Split A → Left (7+, 0−) = 7; Right (0+, 3−) = 3. Split B → Left (5+, 1−) = 6; Right (2+, 2−) = 4. Which split does the tree choose, and by how much does the winning gain beat the loser?
Recall Solution 3.1
Parent : .
Split A — both children pure: . Gain .
Split B: for : . for : . . Gain .
Decision: Split A wins. Gain difference . A perfectly separating split () is unbeatable — it achieves the largest gain possible for any given parent.
Exercise 3.2
A 2-class node is at . Compute its Gini, then compare it to its entropy (from Entropy and Information Gain). Which is smaller, and what does that tell you about the two curves near purity?
Recall Solution 3.2
Gini: . Entropy: . So . Both agree the node is fairly pure (well below their maxima of and ), but their scales differ. The takeaway: Gini and entropy are monotone friends — as one drops toward 0 the other does too — which is why they pick nearly the same splits. Gini just skips the , so it's cheaper.
Exercise 3.3
Someone claims: "Split C gives and Split D gives , so C reduces error more." The parent Gini is . Is the reasoning valid? What single unstated assumption could break it?
Recall Solution 3.3
Gains: C ; D . Since both are compared against the same parent, the smaller does mean the larger gain — so C is better, the ranking is valid. The hidden assumption: both splits act on the same parent node. If C and D were evaluated on different parents (different ), comparing raw values would be meaningless — only gain is comparable across parents. Within one node, minimizing = maximizing gain.
Level 4 — Synthesis
Exercise 4.1
Build the worst-case node. For classes, find the distribution that maximizes Gini, and give that maximum value. Justify why this distribution wins.
Recall Solution 4.1
Gini is , so maximizing means minimizing subject to . Why equal minimizes the sum of squares (the crucial step, justified): By the Cauchy–Schwarz / power-mean inequality, for any numbers with a fixed sum , with equality iff all are equal (this is the QM–AM inequality: the quadratic mean is smallest exactly when all values coincide). So bottoms out at precisely at the uniform distribution. Equivalently: any imbalance moves mass onto a larger whose square grows faster than the freed-up square shrinks, raising . Hence the minimizer of — and therefore the maximizer of — is . This matches the general formula . Answer: uniform , .
Exercise 4.2
Construct 4 items whose class is the XOR of two binary features , then show that (a) splitting on alone gives zero Gini gain, yet (b) a two-step lookahead — split on , then split each child on — separates the data perfectly. This exposes a real greedy-Gini blind spot.
Recall Solution 4.2
The data. Four items, class = (XOR):
| item | class | ||
|---|---|---|---|
| 1 | 0 | 0 | A |
| 2 | 0 | 1 | B |
| 3 | 1 | 0 | B |
| 4 | 1 | 1 | A |
Classes A and B each appear twice, so parent and .
(a) Split on alone — zero gain.
- → items 1,2 = (A, B) → , .
- → items 3,4 = (B, A) → , . A greedy tree sees no improvement from and may refuse it. (By symmetry, splitting on alone is identical: also zero gain.)
(b) Two-step lookahead — perfect separation. Take the split anyway, then split each child on :
- From child {1,2}: split on → gives {item 1 = A} (pure, ); gives {item 2 = B} (pure, ).
- From child {3,4}: split on → gives {item 3 = B} (pure, ); gives {item 4 = A} (pure, ).
All four leaves are pure, so the depth-2 tree classifies every item correctly — total impurity . The single feature looked worthless in isolation, but together with is fully predictive.
Answer: single-feature gain at both first steps, yet the depth-2 tree reaches . Greedy Gini is blind to this feature interaction because it scores each split in isolation. This is the classic XOR problem; deeper trees, feature engineering, or Random Forests mitigate it.
Exercise 4.3
You have a parent with . A candidate split creates two children of equal size (). Child left has . What would make the split's gain exactly ?
Recall Solution 4.3
Equal sizes → each weight is . We want gain . Answer: .
Level 5 — Mastery
Exercise 5.1
Prove that for a 2-class node with proportion (so the other class is ), the Gini impurity equals , and that it is maximized at .
Recall Solution 5.1
Start from the definition with , : Expand : This is a downward parabola in . Its vertex is at (halfway between the roots and ), giving . Proved: , maximal at . The figure below draws exactly this parabola and marks the two roots and the vertex. Read two things off it: (i) the curve is perfectly symmetric about — class 1 at and class 1 at give the same impurity, because " A" and " B" are the same mixed-ness; (ii) the slope is zero at the vertex, confirming is the maximum, and steepest near the pure ends, matching the sensitivity story from Ex. 1.3.

Exercise 5.2
Generalize the maximum. For equally likely classes, prove , and show it increases toward 1 as . Interpret this limit.
Recall Solution 5.2
With for all : As grows: , so . Interpretation: with more equally likely classes you are almost always wrong when guessing by a random draw — the mistake probability climbs toward certainty (1) but never quite reaches it, because there's always a chance your random label happens to match. This is the ceiling Gini can approach but never touch.
Exercise 5.3
Split can never increase impurity (on average). Show that for any binary split of a parent, — i.e. Gini gain is never negative — using the specific numbers: parent (3+, 3−); split into Left (3+, 1−) and Right (0+, 2−). Then state the general principle.
Recall Solution 5.3
Parent : . Left , 4 items: . Right , 2 items: . Then , so gain . ✓ General principle: Gini is a concave function of the class proportions, and equals Gini of the size-weighted mixture of the children's distributions. By Jensen's inequality, the impurity of the mixture is the weighted mixture of impurities: . A split's expected impurity never rises — which is exactly why greedy purity-chasing on training data terminates only at purity, and why we need Overfitting and Pruning to stop early.
Connections
- Gini impurity — the parent note this drill is built on.
- Decision Trees — where these split scores are actually used.
- Entropy and Information Gain — the alternative impurity in Ex. 3.2.
- Overfitting and Pruning — the moral of Ex. 5.3 and the L5 trap.
- Random Forests — one cure for the XOR blind spot in Ex. 4.2.
- Classification vs Regression Trees — regression swaps Gini for variance.