Visual walkthrough — Gini impurity
Everything here is one idea per step, one picture per step. Follow the red object in each figure — red always marks the thing this step is about.
Step 1 — What is a "node"? A bag of coloured balls
WHAT. A decision-tree node is just a group of items, each carrying a class label. We draw it as a bag of coloured balls. Here: 3 red balls and 2 black balls, 5 total.
WHY start here. Before any formula, we need a concrete object to count on. Every quantity below is a count or fraction of balls in this bag — nothing else.
PICTURE. Look at the bag. Red = class 1, black = class 2. The whole formula is a statement about this bag.

Step 2 — The game: draw, hide, guess by a second draw
WHAT. We play a game. Draw one ball and hide its colour (this is the true item). Then draw a second ball independently and copy its colour as our guess. We win if the guess matches the truth, lose if it differs.
WHY this game. Impurity should be small when the bag is one colour (easy, always win) and large when it's mixed (hard, often lose). This "guess by a random second draw" game has exactly that behaviour — so its losing rate is a perfect impurity meter.
PICTURE. Two draws from the same bag: the left draw is the hidden truth, the right draw is our guess. "Independent" means the second draw doesn't know the first.

Here "independent" means: each draw is a fresh random grab from the same proportions . So the guess is red with probability , black with probability — regardless of what the truth was.
Step 3 — Lay out all outcomes as a square
WHAT. Two draws, each red-or-black, give possible outcome pairs: (true, guess). We draw them as a unit square split into strips.
WHY a square. Two independent choices multiply. A square of side 1 lets area = probability: the truth splits the square left-to-right (widths ), the guess splits it top-to-bottom (heights ). Every little rectangle's area is its probability because area = width × height = .
PICTURE. Four tiles. The two diagonal tiles (red–red, black–black) are matches — we win. The two off-diagonal tiles are mismatches — we lose. The red-shaded region is the total losing area.

- — the width of the truth-strip for class .
- — the height of the guess-strip for class .
- — the tile's area = probability of "truth and guess ".
Step 4 — Winning area = sum of squares
WHAT. Add up the areas of the match tiles (the diagonal). A match on class needs truth and guess : area .
WHY square. The same index appears twice — one draw for truth, one for guess, both landing on . Two identical independent probabilities multiplied gives . The square is literally "same colour twice."
PICTURE. Only the two diagonal (match) tiles are highlighted red now. Their areas are and ; together they form the total win area.

- — "add over every class , from 1 to " (here classes).
- — the diagonal tile area for class (a match).
For our bag: .
Step 5 — Gini = losing area =
WHAT. The whole square has area (all probabilities together). Wins fill . Everything left over is losses — and losses are exactly what impurity means.
WHY "1 minus." Total area is 1, and win + lose covers everything with no overlap. So the losing area is simply minus the winning area — no extra work needed. This is the Gini impurity.
PICTURE. The full square (area 1) with the diagonal win-region carved out; the remaining red L-shape is .

We can also read off the off-diagonal tiles directly — the two mismatch tiles have equal areas each: Same number, two ways to see it — because "lose" and "not win" are the same region.
Step 6 — The "per-class" rewrite
WHAT. Split the losing area a different way: group all mismatch tiles by their truth colour. All tiles where truth is class have total width ; the losing fraction of that column is everything except the matching height , i.e. .
WHY. Sometimes we want to know "how much does class contribute to the mistakes?" This rewrite gives each class its own error term. It's the same , re-sliced by column instead of by diagonal.
PICTURE. One column (truth = red, width ) is highlighted; inside it, the losing part has height . Its area is red.

- — probability the true ball is class (column width).
- — probability the guess ball is any other class (the losing height in that column).
Check: . Identical to Step 5 — algebra confirms geometry:
Step 7 — Edge cases: pure, 50/50, and uniform
WHAT. Test the picture at its extremes so no scenario surprises you.
Case A — pure bag (): the square is all one colour, the win-diagonal fills it entirely, losing area . So . Nothing to misclassify.
Case B — fair 2-class mix (): each strip is half-width. Diagonal wins cover ; losses . This is the maximum for 2 classes — the mix fights you hardest.
Case C — 3-class uniform (): now a square, three diagonal tiles of area each, wins , so . This is with .
WHY uniform is worst. Squaring rewards concentration — one big gives a big , shrinking . Spreading probability evenly makes every tiny, so is as small as it can be and as large as it can be.
PICTURE. Three squares side by side: pure (no red), 50/50 (half red), 3-uniform (two-thirds red).

Recall The whole walkthrough retold to a 12-year-old
You have a bag of red and black marbles. Game: grab one, hide its colour, then grab a second marble and just say "I bet the first one is this colour too." You win if they match. If the bag is all red, you always win — boring, zero impurity. If it's half red half black, you're basically flipping a coin — you lose about half the time, that's the worst. To count exactly, imagine a square floor of size 1. Slide the first grab left-to-right and the second grab top-to-bottom, each colour taking up a slice as wide as how common it is. Every floor tile is one possible (first, second) pair, and its size is how likely that pair is. The tiles along the diagonal are "both same colour" — those are your wins. Everything off the diagonal is a loss. Gini impurity is just the total floor area you lose = 1 minus the diagonal wins. Squaring the diagonal is because "same colour twice" means multiplying a colour's slice by itself. A decision tree keeps sorting marbles into smaller bags until each bag is nearly one colour, so the losing area — the Gini — shrinks toward zero.
The one-picture summary
This single figure stacks the whole story: the bag → the outcome square → the win-diagonal → the losing L-shape that is → the two equivalent readings.

Active Recall
What does each little tile in the outcome square represent, and what is its area?
Why do the match tiles have area (a square)?
In the square picture, which region is the Gini impurity?
Why is uniform the maximum-Gini case?
Read off the mismatch tiles for a 60/40 two-class bag.
Connections
- Parent: Gini impurity — the formula this page derives visually.
- Decision Trees — where these bags become tree nodes.
- Entropy and Information Gain — the same square story with instead of squares.
- Classification vs Regression Trees — regression swaps this misclassification area for variance.
- Random Forests — many Gini-grown bags averaged.
- Gini Coefficient (Economics) — same name, different picture (Lorenz curve, not this square).