Sub topic 4
Scoring a split with information gain
A tree has to choose among many candidate splits at every node. This chapter covers the number that ranks them. It assumes you can read basic Python, know what features and labels are, and have seen Gini impurity and entropy as measures of how mixed a node is. We use those measures to score a split, then look at where the score misleads you.
The idea: how much did the split reduce impurity?
A node holds a mix of class labels, and its impurity says how mixed it is. A good split produces children that are purer than the parent. The score is the drop in impurity from the parent to its children:
gain = impurity(parent) - weighted_average(impurity(children))The weights are the fraction of the parent's samples that go to each child. With a left child of n_L samples and a right child of n_R samples out of n in the parent:
gain = I(parent) - (n_L / n) * I(left) - (n_R / n) * I(right)When I is entropy, this quantity is called information gain. When I is Gini, the same formula gives the Gini decrease. The recipe is identical, and only the impurity function changes.
Why the weights matter
A split that sends 99 samples to one child and 1 sample to the other can make the small child perfectly pure. If you averaged the child impurities without weights, that tiny pure child would count as much as the large messy one, and the split would look great. Weighting by sample count makes each child's contribution proportional to how many samples it actually affects.
Implementations also use a second weighting. scikit-learn reports the impurity decrease of a node as a fraction of the whole training set:
weighted_decrease = (N_t / N) * gainHere N_t is the number of samples at the node and N is the size of the full training set. This version lets you compare splits at different depths on one scale. It is also what the min_impurity_decrease hyperparameter and feature importances use. Within a single node, the ordering of candidate splits is the same either way, because N_t / N is a constant there.
A worked example
Take a node with 10 samples: 6 of class 1 and 4 of class 0. Two candidate splits are available.
- Split A sends
[1, 1, 1, 1, 0]left and[1, 1, 0, 0, 0]right. - Split B sends
[1, 1, 1, 1, 1, 0]left and[1, 0, 0, 0]right.
The parent entropy is -(0.6 log2 0.6 + 0.4 log2 0.4) = 0.9710. The parent Gini impurity is 1 - 0.6² - 0.4² = 0.48.
For split A, the left child has entropy 0.7219 and the right has 0.9710. Both children hold half the samples, so the weighted entropy is 0.5 × 0.7219 + 0.5 × 0.9710 = 0.8464. The information gain is 0.9710 - 0.8464 = 0.1245.
For split B, the left child (6 samples) has entropy 0.6500 and the right child (4 samples) has 0.8113. The weighted entropy is 0.6 × 0.6500 + 0.4 × 0.8113 = 0.7145, so the gain is 0.2564. Split B is clearly better, because it isolates a large, mostly-positive group.
The following script reproduces these numbers for both impurity measures:
import math
def entropy(labels):
n = len(labels)
if n == 0:
return 0.0
counts = {}
for y in labels:
counts[y] = counts.get(y, 0) + 1
# Classes with zero count never appear, which handles 0 * log(0) = 0.
return -sum((c / n) * math.log2(c / n) for c in counts.values())
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 impurity_gain(parent, left, right, impurity):
n = len(parent)
weighted = (len(left) / n) * impurity(left) + (len(right) / n) * impurity(right)
return impurity(parent) - weighted
parent = [1] * 6 + [0] * 4
splits = {
"A": ([1, 1, 1, 1, 0], [1, 1, 0, 0, 0]),
"B": ([1, 1, 1, 1, 1, 0], [1, 0, 0, 0]),
}
for name, (left, right) in splits.items():
ig = impurity_gain(parent, left, right, entropy)
gd = impurity_gain(parent, left, right, gini)
print(f"split {name}: information gain = {ig:.4f}, gini decrease = {gd:.4f}")
# split A: information gain = 0.1245, gini decrease = 0.0800
# split B: information gain = 0.2564, gini decrease = 0.1633Both measures rank B above A. That agreement is typical, though the two can disagree on close calls.
Properties worth knowing
- Gain is never negative. Entropy and Gini are concave functions of the class proportions. By Jensen's inequality, the weighted average of the children's impurities cannot exceed the parent's impurity. At worst the gain is zero.
- Gain is zero when the children match the parent. If both children have the same class proportions as the parent, the split taught you nothing, and the score says so.
- The maximum gain is the parent's impurity. That happens when both children are perfectly pure. In the numbers above, no split of that parent can score more than 0.9710 in entropy.
- Units differ. Entropy gain is measured in bits (with log base 2), and Gini decrease is not. You cannot compare an entropy gain to a Gini decrease directly. Compare splits only within the same criterion.
Gini versus entropy in practice
Entropy penalizes mixed nodes a little more steeply near the middle of the probability range, but the two curves are very close. In most datasets they pick the same split at most nodes, and the trained trees perform about the same. Gini is the scikit-learn default (criterion="gini") and is slightly cheaper because it needs no logarithm. Use criterion="entropy" when you want to follow the information-theory framing or when a quick experiment shows a difference on your data. Do not expect large differences.
Gotchas
Bias toward many-valued features
Information gain favors features that can slice the data into many small groups. An identifier column, such as a row ID or a timestamp with unique values, can split the data into one-sample leaves. Each leaf is perfectly pure, so the gain equals the parent's full impurity. The score looks ideal, yet the feature carries no usable signal for new data.
Two approaches address this. Gain ratio, used in C4.5, divides the gain by the split's own entropy (the "split information"), which penalizes splits that produce many branches. The other is practical: remove identifier-like columns, and rely on binary threshold splits and min_samples_leaf, which block one-sample leaves. scikit-learn's trees use only binary splits, which reduces this bias for numeric features but does not remove it. High-cardinality categorical features encoded as many integer values remain a risk.
Greedy scoring cannot see combinations
The score evaluates one split at a time. Consider the XOR pattern, where the label is 1 when exactly one of two binary features is 1. Each feature alone gives a gain of zero, because splitting on either one leaves both children with the same class mix as the parent. A tree might still find the right structure, since it must pick something and ties are broken arbitrarily. Whether it does is luck, and the gain score gives no hint. If you have a variable that matters only in combination with another, expect the greedy search to undervalue it.
Small samples give noisy gains
At deep nodes with few samples, the gain is computed from tiny counts. A split that scores well on 6 samples may be chance. This is one reason stopping rules such as min_samples_split and min_impurity_decrease exist, and they are the subject of a later chapter.
Ties
When two splits have equal gain, scikit-learn picks one based on its feature ordering and the random_state. Different seeds can therefore give different trees on the same data when ties exist. Set random_state to get reproducible results.
Connecting gain to the rest of the algorithm
Information gain is the scoring function inside split search. For each node, the tree algorithm generates candidate splits, scores each with the gain, keeps the best, and recurses on the two children. Its effect is local: the tree never goes back to reconsider a split. That is why it is called greedy, and the XOR case above is where the greed hurts.
The same recipe covers regression. There, the impurity of a node is the variance of its target values (or squared error), and the gain becomes a variance reduction. The weighted-average structure is unchanged.
Exercise
Exercise
A node has 8 samples: 4 of class 1 and 4 of class 0. A candidate split sends [1, 1, 1, 0] to the left child and [1, 0, 0, 0] to the right child. Compute the Gini impurity of the parent, the weighted Gini impurity of the children, and the Gini decrease. Then say what would change if the split were scored using weighteddecrease for a node with Nt = 8 in a training set of N = 80.
Show answer
The parent Gini is 1 - 0.5² - 0.5² = 0.5. The left child has proportions 0.75 and 0.25, so its Gini is 1 - 0.5625 - 0.0625 = 0.375. The right child has proportions 0.25 and 0.75, so its Gini is also 0.375. Both children hold 4 of 8 samples, so the weighted impurity is 0.5 × 0.375 + 0.5 × 0.375 = 0.375. The Gini decrease is 0.5 - 0.375 = 0.125. For the weighteddecrease, multiply by Nt / N = 8 / 80 = 0.1, which gives 0.0125. This scales the score to the whole training set so that nodes at different depths can be compared, but it does not change which split wins within this node.