The parent note showed you the formula G = 1 − ∑ i p i 2 and a couple of examples. Here we exhaust the space : every kind of node you can ever hand to Gini — pure, mixed, uniform, one-class, tiny, huge, 2-class, many-class — plus split decisions and a word problem. By the end you will have seen every case, so nothing on an exam or in real code can surprise you.
Read the Hinglish version if that's easier.
Before working examples, let's list every distinct situation Gini can face. Each row is a "cell". Our examples below will each be tagged with the cell it fills.
Cell
Situation
What makes it special
C1
Perfectly pure node (p = ( 1 , 0 ) )
G hits its floor: exactly 0
C2
Balanced 2-class (p = ( 0.5 , 0.5 ) )
G hits its 2-class ceiling: 0.5
C3
Skewed 2-class (p = ( 0.9 , 0.1 ) )
Between floor and ceiling — the common case
C4
Uniform K -class (p = ( 1/ K , … ) )
G hits its ceiling 1 − 1/ K for that K
C5
Skewed multi-class
Concentration lowers G below the ceiling
C6
Degenerate: one sample, or empty child
Edge input — does the formula even work?
C7
Split decision (compare two splits)
Weighted Gini + gain choose the winner
C8
Real-world word problem
Translate reality → proportions → G
C9
Exam twist: recover p from a given G
Run the formula backwards
Recall Why enumerate cells at all?
Because a "smallest possible" or "largest possible" input often behaves differently from a typical one. A test that never checks the empty child (C6) will crash in real code. Covering the matrix is how you make sure your understanding has no holes .
Let's define the three things we'll use over and over. Two describe a single node; the third scores a whole split.
Definition The three quantities (define every symbol before use)
K = the number of distinct classes in the node (e.g. spam/ham gives K = 2 ; cat/dog/bird/fish gives K = 4 ).
p i = the proportion of items in the node that truly belong to class i . If a node has N items and n i of them are class i , then p i = n i / N . The K proportions add to 1 .
G = 1 − ∑ i = 1 K p i 2 = the misclassification-by-random-guessing rate (built from scratch in the parent note). It runs from 0 (pure) up to 1 − 1/ K (uniform).
Whenever we evaluate a split (a cut that sends the parent's items into several child nodes), we need one more formula. It is worth stating on its own, because Cells C6–C8 all lean on it.
Everything below is just these formulas, applied honestly to every cell.
The picture below plots G for a 2-class node (K = 2 ) as the first class's proportion p slides from 0 to 1 . Watch how the corners of our matrix land on this curve.
Worked example Example 1 — C1 (pure) and C2 (balanced), read off the curve
Node P: 20 spam, 0 ham. Node Q: 10 spam, 10 ham. (Both have K = 2 .)
Forecast: guess each G before reading — one should be the smallest possible, one the largest possible for 2 classes.
Node P proportions: p = ( 20/20 , 0/20 ) = ( 1 , 0 ) .
Why this step? Gini only ever eats proportions, so convert counts first.
∑ p i 2 = 1 2 + 0 2 = 1 , so G P = 1 − 1 = 0 .
Why subtract from 1? ∑ p i 2 is the probability of a correct match; 1 minus it is the mistake rate. Here matching is certain, so mistakes = 0 .
Node Q proportions: p = ( 10/20 , 10/20 ) = ( 0.5 , 0.5 ) .
∑ p i 2 = 0. 5 2 + 0. 5 2 = 0.5 , so G Q = 1 − 0.5 = 0.5 .
Why subtract from 1? Same reason: a 0.5 chance of matching leaves a 0.5 chance of erring — the peak of the red curve, the worst mix for 2 classes.
Verify: G for K = 2 can never exceed 1 − 1/ K = 1 − 1/2 = 0.5 . Node Q sits exactly on that ceiling ✓; Node P sits on the 0 floor ✓.
Worked example Example 2 — C3, a skewed 2-class node
Node: 90 "clicked", 10 "did not click" (100 users, K = 2 ).
Forecast: it's very lopsided toward one class, so G should be small — much closer to 0 than to 0.5 .
p = ( 90/100 , 10/100 ) = ( 0.9 , 0.1 ) .
∑ p i 2 = 0. 9 2 + 0. 1 2 = 0.81 + 0.01 = 0.82 .
Why square? Squaring rewards dominance — the big 0.9 contributes a huge 0.81 .
G = 1 − 0.82 = 0.18 .
Why subtract from 1? 0.82 is the chance of a correct random match, so the leftover 0.18 is the mistake rate we want.
Verify: 0 < 0.18 < 0.5 ✓ — sits on the curve between floor and peak, and much nearer the floor as forecast, because one class dominates.
Worked example Example 3 — C4, uniform multi-class hits the ceiling
Node: an image folder with 25 cats, 25 dogs, 25 birds, 25 fish (100 images, so K = 4 ).
Forecast: all four classes equal → this should be the worst case for K = 4 . Guess the number 1 − 1/ K .
p = ( 0.25 , 0.25 , 0.25 , 0.25 ) .
∑ p i 2 = 4 × 0.2 5 2 = 4 × 0.0625 = 0.25 .
Why 4 × ? Four identical terms, one per class.
G = 1 − 0.25 = 0.75 .
Why subtract from 1? Only a 0.25 chance of a correct random match, leaving a large 0.75 mistake rate.
Verify: ceiling for K = 4 is 1 − 1/4 = 0.75 ✓. Notice: more classes → higher possible impurity (0.5 for K = 2 , 0.667 for K = 3 , 0.75 for K = 4 ).
The next figure shows how the ceiling 1 − 1/ K climbs as classes multiply — and why more classes can be "more confusing".
Worked example Example 4 — C5, skewed 3-class vs the uniform ceiling
Node: 70 red, 20 green, 10 blue (100 items, so K = 3 ).
Forecast: same K = 3 as a uniform node (whose G is 2/3 ≈ 0.667 ), but here one class dominates — so G should be below 0.667 .
p = ( 0.7 , 0.2 , 0.1 ) .
∑ p i 2 = 0.49 + 0.04 + 0.01 = 0.54 .
Why this matters? The dominant 0.7 alone contributes 0.49 — squaring "rewards" its concentration.
G = 1 − 0.54 = 0.46 .
Why subtract from 1? 0.54 is the correct-match chance; 0.46 is what's left over as the mistake rate.
Verify: 0.46 < 0.667 (the uniform 3-class ceiling) ✓. Concentration lowered G exactly as the mnemonic "concentrate to shrink G" predicts.
Real code hands Gini weird nodes: a single sample, or an empty child after a split. Does the formula survive?
Worked example Example 5 — C6, single-sample node and empty child
Node S: exactly 1 sample, class "yes" (binary problem, K = 2 : classes "yes" and "no"). Child E: 0 samples (nothing routed here).
Forecast: a 1-item node can't be mixed, so guess G = 0 . An empty child has no misclassification to make — but watch the weight.
Node S: the one item is "yes", so p = ( 1 , 0 ) (proportion "yes" = 1/1 = 1 , proportion "no" = 0/1 = 0 ).
Why write both entries? In a binary problem there are always K = 2 proportions; showing the explicit 0 for the absent class avoids ambiguity.
∑ p i 2 = 1 2 + 0 2 = 1 , so G S = 1 − 1 = 0 .
Why subtract from 1? A lone item makes matching certain, so the mistake rate is 0 — trivially pure.
Child E: proportions are undefined (0/0 ). But in the weighted-split formula its weight is N c / N = 0/ N = 0 .
Why this saves us? From the [!formula] box, G split = ∑ c N N c G c . An empty child multiplies whatever-garbage-G E -would-be by 0 , contributing nothing. We simply skip empty children in practice.
Consider a split that routes all items to Node S and leaves Child E empty. Then
G split = N N S G S + N 0 G E = 1 ⋅ G S + 0 = G S ,
so gain = G parent − G split = G parent − G S = 0 (since S is the parent here).
Why does gain vanish? The empty term contributes 0 ⋅ G E , so G split equals the single non-empty child's impurity, which equals the parent's — no impurity was removed. The tree therefore refuses such a "split", exactly as it should.
Verify: G S = 1 − 1 = 0 ✓. And N 0 ⋅ ( anything ) = 0 , so the empty child term vanishes ✓ — the formula is safe at the edge, and a one-sided "split" earns zero gain ✓.
Common mistake "An empty child makes
G split undefined, so the code breaks."
Why it feels right: p i = 0/0 looks like division by zero.
The fix: You never evaluate G E — its weight N c / N = 0 zeroes the whole term first. Implementations guard against N c = 0 and drop the child.
This is the whole point of Gini: comparing splits with the weighted-split formula. The figure shows a parent node being cut two different ways.
Worked example Example 6 — C7, choosing between three candidate splits
Parent: 6 positive (+), 6 negative (−), total N = 12 , K = 2 .
Forecast: the split that produces the purest children (children nearest G = 0 ) should win the largest gain.
Parent impurity: p = ( 6/12 , 6/12 ) = ( 0.5 , 0.5 ) , so
G parent = 1 − ( 0. 5 2 + 0. 5 2 ) = 1 − 0.5 = 0.5.
Why first? Gain is measured relative to the parent, so we need this baseline. (Subtract from 1 for the usual reason: 0.5 correct-match chance leaves a 0.5 mistake rate.)
Split A → Left ( 6 + , 0 − ) = 6 , Right ( 0 + , 6 − ) = 6 (a perfect split!).
G L = 1 − 1 2 − 0 2 = 0 ; G R = 1 − 0 2 − 1 2 = 0 . Why 0? Each child is one pure class.
G A = 12 6 ( 0 ) + 12 6 ( 0 ) = 0 .
Why weight by 6/12 ? Each child holds half the data, so each gets half the say (the N c / N from the formula box).
Split B → Left ( 5 + , 1 − ) = 6 , Right ( 1 + , 5 − ) = 6 .
G L = 1 − ( 5/6 ) 2 − ( 1/6 ) 2 = 1 − 25/36 − 1/36 = 10/36 ≈ 0.278 . Why subtract both squares? One term per class inside the child.
G R = same by symmetry ≈ 0.278 .
G B = 12 6 ( 0.278 ) + 12 6 ( 0.278 ) = 0.278 .
Split C → Left ( 4 + , 2 − ) = 6 , Right ( 2 + , 4 − ) = 6 .
G L = 1 − ( 4/6 ) 2 − ( 2/6 ) 2 = 1 − 16/36 − 4/36 = 16/36 ≈ 0.444 .
G R = same ≈ 0.444 .
G C = 0.444 .
Gains (bigger = better), each = G parent − G split :
Split A: 0.5 − 0 = 0.5 .
Split B: 0.5 − 0.278 = 0.222 .
Split C: 0.5 − 0.444 = 0.056 .
Why subtract? Gain measures how much impurity the split removed from the parent.
Decision: Split A wins — it fully separates the classes.
Verify: the purest children (A, both G = 0 ) gave the largest gain (0.5 ); the muddiest (C) gave the smallest (0.056 ) ✓, exactly as forecast. Gains are ordered A > B > C , matching child purity.
Common mistake "Split C's children each have
G ≈ 0.44 , less than the parent's 0.5 , so it's a big improvement."
Why it feels right: every child looks purer than the parent.
The fix: improvement is tiny (0.056 ) because both children are still nearly balanced. Always compute the weighted number and the gain , don't eyeball child Ginis.
Worked example Example 7 — C8, a loan-approval node
A bank's decision-tree node holds 200 loan applicants in K = 3 classes: 150 who repaid ("good"), 40 who defaulted ("bad"), and 10 still "pending". A junior analyst asks: "how impure is this node, and would splitting on 'income > 50k' help?" The income split gives:
High-income child: 120 good, 5 bad, 5 pending (130 people).
Low-income child: 30 good, 35 bad, 5 pending (70 people).
Forecast: the high-income child is dominated by "good", so it should be much purer than the low-income child, which is nearly good/bad balanced.
Parent proportions (K = 3 ): p = ( 150/200 , 40/200 , 10/200 ) = ( 0.75 , 0.20 , 0.05 ) .
G parent = 1 − ( 0.7 5 2 + 0.2 0 2 + 0.0 5 2 ) = 1 − ( 0.5625 + 0.04 + 0.0025 ) = 1 − 0.605 = 0.395 .
Why subtract from 1? 0.605 is the correct-match chance; 0.395 is the mistake rate. One dominant class keeps impurity moderate.
High-income child: p = ( 120/130 , 5/130 , 5/130 ) .
G H = 1 − ( 130 120 ) 2 − 2 ( 130 5 ) 2 = 1 − 0.8521 − 0.0030 = 0.1450.
Why the 2 × ? Two classes ("bad", "pending") share the identical proportion 5/130 .
Low-income child: p = ( 30/70 , 35/70 , 5/70 ) .
G L = 1 − ( 70 30 ) 2 − ( 70 35 ) 2 − ( 70 5 ) 2 = 1 − 0.1837 − 0.25 − 0.0051 = 0.5612.
Weighted split impurity (the [!formula] box, N c / N = 130/200 and 70/200 ):
G split = 200 130 ( 0.1450 ) + 200 70 ( 0.5612 ) = 0.0943 + 0.1964 = 0.2907.
Why weight? The larger, purer high-income child deserves more say than the smaller messy one.
Gain = G parent − G split = 0.395 − 0.2907 = 0.1043 .
Why subtract? It reports how much impurity the income split stripped away.
Verify (with units of "probability"): all G values lie in [ 0 , 1 − 1/3 ] = [ 0 , 0.667 ] ✓. The high-income child (0.145 ) is far purer than the low-income child (0.561 ) as forecast ✓, and the split earns a positive gain ≈ 0.104 ✓ (G split = 0.2907 < G parent = 0.395 ), so yes, splitting on income helps ✓.
Worked example Example 8 — C9, recover the split from a given impurity
Exam question: "A 2-class node (K = 2 ) has Gini impurity G = 0.32 . What are the two class proportions?"
Forecast: Gini is symmetric in p and 1 − p , so expect a matched pair like ( p , 1 − p ) with p somewhere between 0.5 and 1 (since G < 0.5 means it's skewed, not balanced).
Let the proportions be p and 1 − p . Then
G = 1 − p 2 − ( 1 − p ) 2 = 2 p − 2 p 2 = 2 p ( 1 − p ) .
Why this form? For K = 2 , expanding 1 − p 2 − ( 1 − p ) 2 collapses to 2 p ( 1 − p ) — a clean quadratic in one unknown.
Set equal to 0.32 : 2 p ( 1 − p ) = 0.32 ⇒ p − p 2 = 0.16 ⇒ p 2 − p + 0.16 = 0 .
Why rearrange? To put it in standard quadratic form so the quadratic formula applies.
p = 2 1 ± 1 − 4 ( 0.16 ) = 2 1 ± 0.36 = 2 1 ± 0.6 .
Why the quadratic formula? It's the tool that inverts any a x 2 + b x + c = 0 — exactly what "recover p " asks.
So p = 0.8 or p = 0.2 . Both describe the same node : ( 0.8 , 0.2 ) .
Why the same node? The two roots are the two class shares — relabelling which class is "first" cannot change the node.
Verify: G = 1 − 0. 8 2 − 0. 2 2 = 1 − 0.64 − 0.04 = 0.32 ✓. The two roots are mirror images (as forecast) because relabelling the classes can't change impurity.
For 2 classes only (K = 2 ), memorize G = 2 p ( 1 − p ) — an upside-down parabola peaking at p = 0.5 (value 0.5 ) and hitting 0 at p = 0 , 1 . This is the red curve in Figure 1.
Recall
A node has 3 classes with counts ( 80 , 15 , 5 ) out of 100. Gini? ::: 1 − ( 0. 8 2 + 0.1 5 2 + 0.0 5 2 ) = 1 − ( 0.64 + 0.0225 + 0.0025 ) = 0.335
Two children with sizes 40 and 60 have Ginis 0.1 and 0.5 . Weighted split Gini? ::: 100 40 ( 0.1 ) + 100 60 ( 0.5 ) = 0.04 + 0.30 = 0.34
Which cell does an empty child fall in, and why doesn't it break the formula? ::: C6 (degenerate); its weight N c / N = 0 zeroes the term before G c is ever evaluated.
A 2-class node has G = 0.5 . What are its proportions? ::: ( 0.5 , 0.5 ) — the unique 2-class maximum.
Why does a skewed 3-class node have lower G than a uniform 3-class node? ::: Squaring rewards concentration, so a dominant class raises ∑ p i 2 , lowering G = 1 − ∑ p i 2 .
Gini impurity — parent note (formula, derivation, mnemonics).
Decision Trees — where these split comparisons (C7) actually run.
Entropy and Information Gain — the same examples with − ∑ p log p instead.
Overfitting and Pruning — why chasing the pure children of C1/Split A can backfire.
Random Forests — many Gini-grown trees averaged.
Classification vs Regression Trees — regression swaps Gini for variance.
Gini Coefficient (Economics) — different concept, same name (see parent's mistake box).