Gini Impurity
Learn how Gini impurity measures class mixing in a node.
Big Idea
In the previous lesson, we learned where a decision tree is allowed to split the data. After a threshold divides patients into groups, the tree needs a way to measure how mixed each group is.
Gini Impurity answers that question for a single group.
- A pure group contains only one class (all patients have the same heart disease label).
- A mixed group contains more than one class (some patients have heart disease, some do not).
Gini Impurity does not score an entire split yet. It does not combine left and right groups. It only measures the impurity of one group at a time.
Medical Example
Suppose a threshold on Age creates this left group from our heart disease dataset:
| Patient | Age | Heart Disease |
|---|---|---|
| P1 | 42 | 0 |
| P2 | 45 | 0 |
| P3 | 45 | 0 |
| P4 | 48 | 0 |
| P5 | 50 | 0 |
This group contains:
- 5 patients
- 5 with no heart disease (class 0)
- 0 with heart disease (class 1)
Every patient has the same label. This is a pure group.
Formula
For K classes, Gini Impurity is:
Gini=1−k=1∑Kpk2For binary classification (heart disease: 0 or 1):
Gini=1−(p02+p12)where
- p0 is the proportion of class 0 (no heart disease)
- p1 is the proportion of class 1 (heart disease)
Worked Example 1: Pure Group
For the left group above:
p0=55=1 p1=50=0Then:
Gini=1−(12+02)=0A Gini score of 0 means the group is perfectly pure. If you predicted the majority class for every patient in this group, you would always be correct.
Worked Example 2: Mixed Group
Now consider a different group:
| Patient | Age | Heart Disease |
|---|---|---|
| P4 | 48 | 0 |
| P5 | 50 | 0 |
| P6 | 53 | 1 |
| P7 | 55 | 1 |
This group contains:
- 4 patients
- 2 with no heart disease
- 2 with heart disease
Compute the proportions:
p0=42=0.5 p1=42=0.5Then:
Gini=1−(0.52+0.52)=0.5For binary classification, 0.5 is the highest possible Gini value. A 50/50 split is as mixed as a group can be.
Intuition
Think of Gini as a mixing score for one group:
- Gini = 0 — perfectly pure (one class only)
- Gini close to 0 — mostly one class, with a few exceptions
- Gini close to 0.5 — highly mixed (nearly half and half)
Lower Gini means a cleaner, more predictable group.
▸Common Mistake
A common mistake is computing Gini using the whole dataset instead of only the group being evaluated.
Gini Impurity is always calculated for one group at a time — for example, only the left group, or only the right group. Each group gets its own Gini score.
▸Key Takeaway
Gini Impurity tells us how mixed one group is.
It does not yet tell us whether a split is good or bad. In the next lesson, Weighted Gini, we will combine the Gini scores of the left and right groups to score an entire split.
Interactive
Gini Impurity Calculator
Adjust class counts to see how Gini impurity changes for a two-class split.
Total samples
10
p₀
0.600
p₁
0.400
Gini
0.480
Formula
Gini = 1 − (p₀² + p₁²) = 1 − (0.600² + 0.400²) = 0.480
Practice
Try It Yourself
Open the practice lab to complete the starter code in the notebook.
Knowledge Check
Quick Quiz
What is the Gini impurity of a perfectly pure node?
Summary
Key Takeaways
- Gini impurity measures how mixed the classes are in a node.
- A pure node has Gini impurity of 0.
- Maximum impurity for two classes occurs when classes are evenly split.