Entropy and Information Gain
Think of the game "20 questions." A good first question is "Is it alive?", not "Is it a left-handed penguin?". The good question splits the possibilities roughly in half, whatever the answer. The bad one almost always gets "no" and teaches you almost nothing. To build good trees we need to measure, in numbers, how much a question tells us. Claude Shannon worked out how in 1948.
Surprise and information
How much information do you gain when an event with probability \(p\) happens? Shannon wanted a measure \(I(p)\) with three properties:
- Certain events carry no information: \(I(1) = 0\).
- Rarer events carry more: \(I\) decreases in \(p\).
- Information from independent events adds: \(I(p_1 p_2) = I(p_1) + I(p_2)\).
The only function with these properties is the logarithm:
$$ I(p) = -\log_2 p \quad \text{bits.} $$A fair coin flip gives \(-\log_2 \tfrac12 = 1\) bit. Learning which of 8 equally likely hostels an order came from gives \(-\log_2 \tfrac18 = 3\) bits, which is exactly three yes/no questions.
Entropy: expected surprise
The entropy of a random variable is its average information content. It measures how uncertain we are before seeing the outcome:
$$ \boxed{\,H(X) = -\sum_{k} P(x_k) \log_2 P(x_k)\,} \qquad (\text{with } 0 \log 0 = 0). $$For a Boolean variable that is true with probability \(p\), write \(B(p)\):
$$ B(p) = -p\log_2 p - (1-p)\log_2(1-p). $$| Distribution | Entropy | Meaning |
|---|---|---|
| \(p = 0\) or \(1\) | 0 bits | No uncertainty: you already know |
| \(p = 0.5\) (fair coin) | 1 bit | Maximum uncertainty for 2 outcomes |
| \(p = 0.99\) | 0.08 bits | Almost certain |
| Uniform over \(k\) outcomes | \(\log_2 k\) bits | Maximum for \(k\) outcomes |
For a set \(S\) of training examples with \(p\) positive and \(n\) negative ones, the entropy of the labels is \(H(S) = B\!\left(\frac{p}{p+n}\right)\). A pure set (all one class) has entropy 0. A 50/50 set has entropy 1. That's exactly what we need: a measure of how "mixed" a node is.
14 examples, 9 Yes and 5 No:
$$ H(S) = -\tfrac{9}{14}\log_2\tfrac{9}{14} - \tfrac{5}{14}\log_2\tfrac{5}{14} = 0.643(0.637) + 0.357(1.485) = 0.410 + 0.530 = 0.940 \text{ bits}. $$Log tricks for exams without a calculator: \(\log_2 x = \ln x / \ln 2 = \log_{10} x / 0.301\). Memorize \(B(1/4) = B(3/4) = 0.811\), \(B(1/3) = B(2/3) = 0.918\), \(B(2/5) = B(3/5) = 0.971\) and \(B(1/2) = 1\).
Information gain
Now ask a question: split \(S\) on attribute \(A\). Each value \(v\) of \(A\) gets a subset \(S_v\). After the split, the expected remaining entropy is the weighted average of the children's entropies, called the remainder:
$$ \text{Remainder}(A) = \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|}\, H(S_v). $$The information gain is how much the split reduces entropy:
$$ \boxed{\,\text{Gain}(S, A) = H(S) - \sum_{v} \frac{|S_v|}{|S|} H(S_v)\,} $$ID3 splits on the attribute with the highest gain: the question whose answer, on average, leaves the least uncertainty.
| Outlook | Examples | [+, −] | \(H(S_v)\) |
|---|---|---|---|
| Sunny | 5 | [2+, 3−] | \(B(2/5) = 0.971\) |
| Overcast | 4 | [4+, 0−] | 0 (pure!) |
| Rain | 5 | [3+, 2−] | \(B(3/5) = 0.971\) |
Similarly: Humidity: High [3+,4−] (\(H=0.985\)), Normal [6+,1−] (\(H=0.592\)), so Gain = \(0.940 - \tfrac{7}{14}0.985 - \tfrac{7}{14}0.592 = 0.152\). Wind: Weak [6+,2−] (\(H=0.811\)), Strong [3+,3−] (\(H=1\)), so Gain = \(0.940 - \tfrac{8}{14}0.811 - \tfrac{6}{14}1 = 0.048\). Temperature: Gain = 0.029. Outlook wins.
Check every number above with the calculator. Choose a preset or type your own counts:
In code
import math
from collections import Counter
ATTRS = ["Outlook", "Temp", "Humidity", "Wind"]
DATA = [("Sunny","Hot","High","Weak","No"), ("Sunny","Hot","High","Strong","No"),
("Overcast","Hot","High","Weak","Yes"), ("Rain","Mild","High","Weak","Yes"),
("Rain","Cool","Normal","Weak","Yes"), ("Rain","Cool","Normal","Strong","No"),
("Overcast","Cool","Normal","Strong","Yes"), ("Sunny","Mild","High","Weak","No"),
("Sunny","Cool","Normal","Weak","Yes"), ("Rain","Mild","Normal","Weak","Yes"),
("Sunny","Mild","Normal","Strong","Yes"), ("Overcast","Mild","High","Strong","Yes"),
("Overcast","Hot","Normal","Weak","Yes"), ("Rain","Mild","High","Strong","No")]
def entropy(rows):
counts = Counter(r[-1] for r in rows)
n = len(rows)
return -sum(c / n * math.log2(c / n) for c in counts.values())
def info_gain(rows, attr):
i = ATTRS.index(attr)
remainder = 0.0
for value in set(r[i] for r in rows):
subset = [r for r in rows if r[i] == value]
remainder += len(subset) / len(rows) * entropy(subset)
return entropy(rows) - remainder
print("H(S) = %.3f" % entropy(DATA))
for a in ATTRS:
print(f"Gain(S, {a:8s}) = {info_gain(DATA, a):.3f}")
sunny = [r for r in DATA if r[0] == "Sunny"]
print("\nInside the Sunny branch, H = %.3f" % entropy(sunny))
for a in ["Temp", "Humidity", "Wind"]:
print(f"Gain(Sunny, {a:8s}) = {info_gain(sunny, a):.3f}")H(S) = 0.940
Gain(S, Outlook ) = 0.247
Gain(S, Temp ) = 0.029
Gain(S, Humidity) = 0.152
Gain(S, Wind ) = 0.048
Inside the Sunny branch, H = 0.971
Gain(Sunny, Temp ) = 0.571
Gain(Sunny, Humidity) = 0.971
Gain(Sunny, Wind ) = 0.020
Inside the Sunny branch, Humidity has gain 0.971, equal to the branch's entire entropy. That means Humidity splits the Sunny days perfectly: High → all No, Normal → all Yes.
A flaw of information gain, and the fix
Suppose we add an attribute "Date" with a unique value for every day. Splitting on Date gives 14 pure children with one example each, so Remainder = 0 and the gain is the maximum possible, 0.940. ID3 would happily put Date at the root. It's a perfect fit to the training data and useless for prediction, which is overfitting again. Information gain is biased toward attributes with many values.
C4.5's fix is the gain ratio. Divide by the "split information," the entropy of the split itself:
$$ \text{SplitInfo}(S, A) = -\sum_v \frac{|S_v|}{|S|}\log_2\frac{|S_v|}{|S|}, \qquad \text{GainRatio} = \frac{\text{Gain}(S,A)}{\text{SplitInfo}(S,A)}. $$Date has SplitInfo = \(\log_2 14 = 3.81\), which heavily penalizes its gain ratio. Outlook splits the 14 days into groups of 5, 4 and 5, so its SplitInfo is the entropy of those proportions: 1.577. Its gain ratio is therefore \(0.247/1.577 = 0.157\).
Gini impurity: the other popular measure
CART (and scikit-learn's default) uses Gini impurity instead of entropy:
$$ \text{Gini}(S) = 1 - \sum_k p_k^2. $$It is the probability of mislabelling a random example if you label it randomly according to the node's class distribution. It's 0 for a pure node and 0.5 for a 50/50 binary node. Gini and entropy have similar shapes and usually choose the same splits. Gini is slightly cheaper to compute (no logarithms).
- Entropy is expected surprise, an expectation over a probability distribution (Ch 4).
- Information gain = how much observing \(A\) is expected to reduce uncertainty about the class. That is the mutual information \(I(A; \text{Class})\). The value of information (§5.1) is the same idea measured in utility instead of bits.
- Cross-entropy, the standard loss for neural-network classifiers, is a close relative of entropy.