Sub topic 3
Measuring node purity with Gini and entropy
This chapter assumes you know basic Python, what features and labels are, and how to compute class proportions. It covers how a tree scores a node. Scoring a split comes in the next chapter.
Why purity matters
A tree wants each leaf to contain mostly one class. A node where every sample has the same label is pure. A node with a mix of labels is impure. An impurity measure turns the class mix of a node into one number. Zero means pure, and larger values mean more mixed.
Both measures below use the class proportions p_k in a node, where p_k is the fraction of samples belonging to class k.
Gini impurity
Gini impurity is the probability that a randomly chosen sample would be mislabeled if you labeled it by drawing a random label from the node's own class distribution.
Gini = 1 - sum(p_k ** 2)For two classes, the range is 0 to 0.5. For K classes, the maximum is 1 - 1/K.
Entropy
Entropy measures the uncertainty in the class label, in bits when you use base-2 logarithms.
Entropy = - sum(p_k * log2(p_k))Treat 0 * log2(0) as 0. For two classes, the range is 0 to 1. For K classes, the maximum is log2(K).
Worked example
A node holds 10 samples: 8 of class A and 2 of class B. So p_A = 0.8 and p_B = 0.2.
- Gini:
1 - (0.64 + 0.04) = 0.32 - Entropy:
-(0.8 * log2(0.8) + 0.2 * log2(0.2)), which is about0.722
A 5/5 node is the worst case for two classes. Its Gini is 0.5 and its entropy is 1.0.
import math
def gini(labels):
n = len(labels)
if n == 0:
return 0.0
counts = {}
for y in labels:
counts[y] = counts.get(y, 0) + 1
return 1.0 - sum((c / n) ** 2 for c in counts.values())
def entropy(labels):
n = len(labels)
if n == 0:
return 0.0
counts = {}
for y in labels:
counts[y] = counts.get(y, 0) + 1
return -sum((c / n) * math.log2(c / n) for c in counts.values())
node = ["A"] * 8 + ["B"] * 2
print(round(gini(node), 3)) # 0.32
print(round(entropy(node), 3)) # 0.722
print(gini(["A"] * 5 + ["B"] * 5), entropy(["A"] * 5 + ["B"] * 5)) # 0.5 1.0Comparing the two
| Property | Gini | Entropy |
|---|---|---|
| Formula uses | Squares | Logarithms |
| Maximum, 2 classes | 0.5 | 1.0 |
| Cost | Slightly cheaper | Slightly costlier |
| scikit-learn setting | `criterion="gini"` (default) | `criterion="entropy"` |
Both are zero for a pure node, both peak at an even mix, and both fall as one class dominates. In practice they usually pick the same or very similar splits. Entropy penalizes mixed nodes a little more strongly. Gini is the default because it avoids computing logarithms.
Common errors
- Comparing raw values across measures. A Gini of 0.32 and an entropy of 0.72 describe the same node. Do not compare them with each other.
- Forgetting the empty node. Without a guard for
n == 0, you divide by zero. The functions above return 0.0. - Calling
log2(0). This happens if you loop over all possible classes instead of only the classes present. Count only the labels that appear. - Treating impurity as accuracy. A node can have low impurity and still be a poor predictor if it has very few samples.
Exercise
A node has 12 samples: 6 of class A, 3 of class B, and 3 of class C. Compute its Gini impurity and its entropy in bits.
Show answer
The proportions are 0.5, 0.25, and 0.25. Gini = 1 - (0.25 + 0.0625 + 0.0625) = 0.625. Entropy = -(0.5 log2(0.5) + 2 0.25 * log2(0.25)) = 0.5 + 1.0 = 1.5 bits.
Exercise
Why does a 9/1 node have lower impurity than a 6/4 node under both measures?
Show answer
Both measures are smallest when one class dominates and largest when classes are evenly mixed. The 9/1 node has Gini 0.18 and entropy about 0.469. The 6/4 node has Gini 0.48 and entropy about 0.971.