Intuition The one core idea
A decision tree wants each final group of data to contain only ONE kind of thing (all apples, all oranges). Gini impurity is a single number between 0 and 1 that says "how mixed-up is this group?" — and the whole topic is just building that number from probabilities and then using it to pick good splits.
This page assumes you know nothing . Before you can read the parent note's formula G = 1 − ∑ i = 1 K p i 2 , you must first know what a proportion is, what that stretched-out S (∑ ) means, why we square , and what a "node" even looks like. We build every one of those, in order, with pictures.
Everything in this topic is about sorting things into boxes . Look at the figure: a big box of mixed circles gets split into two smaller boxes. Each box is a node .
Definition Node (a box of items)
A node is just a group of data items sitting together — drawn as a box. The root node is the whole dataset; after a split, each smaller box is a child node ; a box we stop splitting is a leaf .
Picture: the amber box at the top is the parent; the two cyan boxes below are its children.
We need this word because Gini impurity is always computed for one node at a time . There is no "Gini of the whole tree" — only the Gini of each box.
Before proportions, we must count.
Why the topic needs it: when we compare splits, a big child and a tiny child should not count equally. Counting lets us weight them (Section 6).
The circles come in colors. In machine learning a "color" is called a class — the label we're trying to predict (apple/orange, spam/not-spam, +/−).
K , and the index i
A class is one category an item can belong to (e.g. "apple").
K = the total number of different classes in the node.
i = a counter that walks through the classes one at a time: i = 1 , 2 , … , K . It's a placeholder, like saying "for each color…". It has no value of its own until you plug a class in.
Now the star symbol of the whole topic:
Intuition Why proportions, not raw counts?
Gini asks "if I grab a random item, what color is it likely to be?" — that's a probability , and a probability is a proportion. Two boxes with the same mix (60/40) should get the same impurity even if one holds 10 items and the other 1000. Using p i (a fraction) makes the number about mix , not size .
Two facts about proportions that we'll lean on:
Every p i is between 0 and 1 (you can't have a negative share or more than 100%).
They all add up to 1 , because every item belongs to exactly one class. This is written ∑ i p i = 1 — which needs the next symbol.
This stretched letter scares beginners. It is just "add up a list."
∑ (sigma — "add them all")
∑ i = 1 K ( something involving i )
reads: "let i be 1 , compute the thing; let i be 2 , compute it; … up to K ; then add all those results together."
i = 1 under the sign = where to start.
K on top = where to stop.
The bit after it = what to compute each time.
Worked example Unrolling a sum
Suppose K = 3 and p 1 = 0.5 , p 2 = 0.3 , p 3 = 0.2 .
∑ i = 1 3 p i = p 1 + p 2 + p 3 = 0.5 + 0.3 + 0.2 = 1 ✓
That "= 1 " is the promise from Section 2: the shares always fill the whole pie.
Why the topic needs it: the Gini formula must combine all the classes into one number. ∑ is the tool that says "loop over every class and total it up."
p i 2 (a proportion times itself)
p i 2 = p i × p i . Since p i is a fraction below 1, squaring makes it smaller — and it shrinks more the smaller p i already is.
Look at the curve in the figure: plotting p 2 against p bends downward. A big share (say 0.9 ) barely shrinks (0.81 ); a small share (0.1 ) collapses (0.01 ).
Intuition Why squaring is the perfect trick
p i 2 is exactly the chance that two independent random grabs both land on class i (grab-and-copy game from the parent note): first grab is class i with chance p i , second grab agrees with chance p i , so both = p i × p i = p i 2 .
Squaring rewards dominance : one big color makes ∑ p i 2 large (so impurity G = 1 − ∑ p i 2 is small = pure). Spreading colors evenly makes every p i 2 tiny, so ∑ p i 2 is small and G is large = mixed. That single choice — square then subtract from 1 — is the entire mechanism.
You now own every symbol. Read the parent's formula left to right:
Quick sanity checks (all cases):
All one class: p = ( 1 , 0 ) , ∑ p i 2 = 1 , so G = 1 − 1 = 0 . Pure box → never wrong. ✔
Fifty-fifty (2 classes): p = ( 0.5 , 0.5 ) , ∑ = 0.25 + 0.25 = 0.5 , G = 0.5 . Worst 2-class mix. ✔
Equal thirds (3 classes): ∑ = 3 × ( 1/3 ) 2 = 1/3 , G = 2/3 . Worst 3-class mix. ✔
A split makes several children. To score it we need one number, so we average the children's Ginis — but weighted by how many items each holds.
Intuition Why weight, in one picture
Imagine total mistakes across ALL items. A tiny pure child (G c = 0 ) removes only a few possible mistakes; a big messy child adds many. Weighting by N c / N measures expected mistakes per item over the whole parent — exactly what we want to minimise.
The tree then picks the split with the smallest G split , i.e. the biggest Gini gain = G parent − G split .
Classes and index i up to K
Proportion pi equals count over N
Summation sign adds a list
Squaring pi rewards dominance
Gini G equals 1 minus sum of squares
Decision Tree picks best split
Test yourself — reveal only after you've answered aloud.
What is a "node" in a decision tree? A group of data items (a box); the root is the whole dataset, children are the smaller boxes after a split, a leaf is a box we stop splitting.
What does N mean, and N c ? N = total items in the node; N c = number of items in child node c .
What is a class, and what is K ? A class is a category/label an item can have; K is how many distinct classes exist in the node.
What does the index i do? It's a counter that walks through the classes 1 , 2 , … , K — a placeholder meaning "for each class".
Define the proportion p i in words and formula. The fraction of the node's items in class i : p i = ( count of class i ) / N .
Why must ∑ i p i = 1 ? Every item belongs to exactly one class, so the shares fill the whole pie.
What does ∑ i = 1 K tell you to do? Compute the expression for i = 1 up to i = K and add all results together.
What is p i 2 probabilistically? The chance two independent random grabs both land on class i .
Why do we square instead of just using p i ? Squaring rewards dominance — one big share makes ∑ p i 2 large (pure, low G ); an even spread makes it small (mixed, high G ).
Why do we weight children by N c / N in a split? So the score reflects total expected mistakes per item across the whole parent, not letting a tiny child count as much as a big one.