Sub topic 4
Scoring a split with information gain
This chapter assumes you know what Gini impurity and entropy measure for a single node, and that a tree grows by greedy recursive partitioning. Here we turn those node-level numbers into a score for a whole split.
From node purity to split quality
A candidate split sends the samples in a parent node to two children. A good split makes the children purer than the parent. Information gain measures that improvement:
gain = impurity(parent) − Σ (nchild / nparent) × impurity(child)
The sum is a weighted average. Each child counts in proportion to its share of the samples, so a tiny pure child cannot hide a large messy one. When the impurity is entropy, the result is called information gain. When it is Gini, the same formula gives the Gini decrease. Many texts use "weighted impurity decrease" for both.
A worked example
Take a parent with 6 samples of class A and 4 of class B. Split it into a left child (4 A, 1 B) and a right child (2 A, 3 B). Each child holds half the samples.
With Gini:
- Parent: 1 − 0.6² − 0.4² = 0.48
- Left: 1 − 0.8² − 0.2² = 0.32
- Right: 1 − 0.4² − 0.6² = 0.48
- Weighted children: 0.5 × 0.32 + 0.5 × 0.48 = 0.40
- Gain: 0.48 − 0.40 = 0.08
With entropy (base 2), the parent is 0.9710 bits, the left child is 0.7219 and the right child is 0.9710. The weighted children score 0.8464, so the gain is about 0.1245 bits. The numbers differ, but both criteria rate this split as a modest improvement.
Computing it in code
from math import log2
def gini(labels):
n = len(labels)
return 1.0 - sum((labels.count(c) / n) ** 2 for c in set(labels))
def entropy(labels):
n = len(labels)
return -sum((labels.count(c) / n) * log2(labels.count(c) / n) for c in set(labels))
def gain(parent, children, impurity):
n = len(parent)
weighted = sum(len(ch) / n * impurity(ch) for ch in children)
return impurity(parent) - weighted
parent = ["A"] * 6 + ["B"] * 4
left = ["A"] * 4 + ["B"] * 1
right = ["A"] * 2 + ["B"] * 3
print("gini gain:", round(gain(parent, [left, right], gini), 4)) # 0.08
print("entropy gain:", round(gain(parent, [left, right], entropy), 4)) # 0.1245The gain function takes the impurity function as an argument, so you can compare criteria on the same split.
How the score is used
At each node, the tree evaluates the gain of every candidate split and keeps the largest. This is the "best" in best split, and it is greedy. The tree never checks whether a weaker split now would allow better splits later.
scikit-learn exposes this score in two places:
criterion="gini"orcriterion="entropy"chooses the impurity function.min_impurity_decreaserejects splits whose gain is too small. Internally, the decrease is scaled by the node's share of the whole training set (N_t / N). A node deep in the tree needs a larger per-sample improvement than the same raw gain near the root.
Common errors
- Forgetting to weight the children. An unweighted average rewards splits that peel off one or two pure samples.
- Mixing impurities. Gini gains and entropy gains have different scales, so a threshold chosen for one does not transfer to the other.
- Preferring features with many distinct values. Information gain tends to favor categorical features with many categories, because tiny children are easily pure. C4.5 addresses this with gain ratio, which divides the gain by the entropy of the split proportions.
- Treating a positive gain as proof the split is useful. Gain is always zero or positive on training data, so a split can still fit noise. Later chapters handle that with stopping rules and pruning.
Exercise
A parent node has 4 samples of class A and 4 of class B. A split produces a left child with 3 A and 1 B, and a right child with 1 A and 3 B. What is the Gini gain?
Show answer
Parent Gini = 1 − 0.5² − 0.5² = 0.5. Each child has Gini = 1 − 0.75² − 0.25² = 0.375. The children have equal size, so the weighted impurity is 0.375. The gain is 0.5 − 0.375 = 0.125.