Machine Learning Algorithms

Ch.38: Entropy, Information Gain, and Gini Impurity

By Ayush Arora8 min read

Inspired by: YouTube

Last post left two questions open: which feature should a node split on, and when should splitting stop. This post answers the first one, and along the way builds the tool (entropy, and its faster cousin Gini impurity) that later posts will reuse to answer the second.


1. The Play Tennis Dataset

The worked example is the classic dataset used to teach this: 14 days, four weather features, and whether tennis was played.

outlooktemperature (°F)humiditywindplay
sunny85highweakno
sunny80highstrongno
overcast83highweakyes
rain70highweakyes
rain68normalweakyes
rain65normalstrongno
overcast64normalstrongyes
sunny72highweakno
sunny69normalweakyes
rain75normalweakyes
sunny75normalstrongyes
overcast72highstrongyes
overcast81normalweakyes
rain71highstrongno

9 days say yes, 5 say no. outlook, humidity, and wind are categorical; temperature is numeric and gets its own section later. The question this post answers: of outlook, humidity, and wind, which one should the root node split on first?

2. What Is Entropy

Entropy measures how mixed up, or "disordered," a set of labels is. A set that's all one class is perfectly ordered, entropy 0. A set split evenly between two classes is maximally disordered, since knowing nothing else, a guess is a genuine coin flip. Entropy is highest exactly there.

For a set SS with classes 1n1 \ldots n, each making up a fraction pip_i of SS:

H(S)=i=1npilog2piH(S) = -\sum_{i=1}^{n} p_i \log_2 p_i

For the binary case (two classes, pp and 1p1-p), this simplifies to H(p)=plog2p(1p)log2(1p)H(p) = -p\log_2 p - (1-p)\log_2(1-p), which looks like this across every possible pp:

Line chart of entropy as a function of probability p, from 0 to 1. The curve starts at 0, rises smoothly to a peak of 1.0 exactly at p=0.5, marked with a red dot, then falls symmetrically back to 0 at p=1

Two things worth noting directly off this curve: entropy is 0 at both ends (p = 0 or p = 1, a pure set) and peaks at exactly 1 when p = 0.5 (a perfect 50/50 mix). It's also perfectly symmetric, H(0.3) = H(0.7), entropy only cares how mixed the set is, not which class happens to be in the majority.

3. Computing Entropy on Play Tennis

The root node holds all 14 rows: 9 yes, 5 no, so pyes=9/14p_{yes} = 9/14, pno=5/14p_{no} = 5/14.

H(S)=914log2914514log25140.940H(S) = -\frac{9}{14}\log_2\frac{9}{14} - \frac{5}{14}\log_2\frac{5}{14} \approx 0.940

Close to the maximum of 1, as expected: 9-vs-5 is fairly close to an even split.

4. Information Gain

Entropy alone describes a single set. To decide which feature to split on, compare the parent's entropy against the weighted-average entropy of the children that split would produce, that difference is information gain:

IG(S,A)=H(S)vvalues(A)SvSH(Sv)IG(S, A) = H(S) - \sum_{v \in \text{values}(A)} \frac{|S_v|}{|S|} H(S_v)

A feature that produces children with low entropy (each child mostly one class) scores a high information gain; a feature that barely changes how mixed the children are scores close to zero. Working this out for outlook, which splits the 14 rows into three groups:

outlook valuerowsyes / noentropy
sunny52 / 30.971
overcast44 / 00.000
rain53 / 20.971

Hweighted=514(0.971)+414(0.000)+514(0.971)0.694H_{\text{weighted}} = \frac{5}{14}(0.971) + \frac{4}{14}(0.000) + \frac{5}{14}(0.971) \approx 0.694 IG(S,outlook)=0.9400.6940.247IG(S, \text{outlook}) = 0.940 - 0.694 \approx 0.247

Running the same computation for the other two categorical features:

Bar chart of information gain for three features: outlook at 0.247, humidity at 0.152, and wind at 0.048. Outlook's bar is clearly the tallest

outlook wins by a clear margin, which lines up with what the raw numbers already hinted at: overcast alone perfectly separates 4 pure yes rows, dragging the weighted entropy down more than any other feature manages. The root node splits on outlook. The overcast branch is already done (entropy 0, a pure leaf); sunny and rain each still have mixed labels and need a further split, chosen by running this exact same information-gain computation again, but only on the rows within that branch.

5. Observations

A few things fall out of working through this by hand, worth stating explicitly since they generalize to every tree the rest of this series builds:

6. Entropy vs Probability, Revisited

It's worth re-reading the entropy curve from Section 2 with a decision-tree lens rather than a pure-math one: a node is "good" (low entropy) exactly when it's lopsided toward one class, and "bad" (high entropy) exactly when it's a coin flip. Every split a tree makes is, in effect, trying to drag each child node's pp away from 0.5 and toward one of the two edges of that curve.

7. Gini Impurity

Entropy involves a logarithm for every class, computed at every candidate split, for every node, this adds up. Gini impurity measures almost the same thing, "how mixed is this set," with cheaper arithmetic:

Gini(S)=1i=1npi2\text{Gini}(S) = 1 - \sum_{i=1}^{n} p_i^2

Overlaying both curves for the binary case shows how similar they actually are:

Two overlapping curves against probability p from 0 to 1: entropy in blue, peaking at 1.0 at p=0.5, and Gini impurity in green, peaking at 0.5 at the same point p=0.5. Both curves are 0 at p=0 and p=1 and rise and fall symmetrically

Same shape, same zeros at the pure ends, same peak location at p = 0.5, just a lower ceiling (0.5 instead of 1) and no logarithm to compute. This is why CART, the specific algorithm named at the end of the last post, defaults to Gini impurity rather than entropy: in practice the two pick nearly identical splits almost all the time, but Gini is faster to compute at scale. Running the root-node numbers through Gini instead of entropy confirms the same feature wins:

Gini(S)=1(914)2(514)20.459\text{Gini}(S) = 1 - \left(\frac{9}{14}\right)^2 - \left(\frac{5}{14}\right)^2 \approx 0.459

For outlook: Gini(sunny) = 0.480, Gini(overcast) = 0.000, Gini(rain) = 0.480, weighted average ≈ 0.343, a Gini-based "gain" of 0.459 − 0.343 ≈ 0.116, still the largest of the three features. The specific numbers differ from entropy's, but the winner doesn't.

8. Handling Numerical Data

Section 4 (of the previous post) left one question open: for a numeric feature, which threshold do you actually split on? The answer is a search, not a formula: sort the feature's unique values, take the midpoint between each consecutive pair as a candidate threshold, and compute information gain for that candidate exactly like a categorical split, < threshold versus ≥ threshold playing the role of the two branches.

Running that sweep over temperature in the Play Tennis data (14 rows, unique values 64 through 85, giving 13 candidate midpoints):

Line chart of information gain against candidate temperature thresholds ranging from about 64 to 84 degrees. The curve fluctuates and reaches its highest point at 84 degrees, marked with a red dot

The best candidate, temperature < 84, is picked exactly the way outlook was picked among categorical features, by comparing information gain across every candidate and keeping the largest. The only real difference from the categorical case is volume: a categorical feature has one gain to compute, a numeric feature has one gain per candidate threshold, which is also why Gini's cheaper arithmetic tends to matter more in practice than the entropy-vs-probability curve alone would suggest, this search runs at every single node, for every numeric feature, throughout training.

With this, the two open questions from the intuition post are down to one: choosing which feature and threshold to split on is fully specified by entropy/Gini and information gain. Deciding when to stop splitting, and what to do about a tree that overfits by splitting all the way down, is still ahead.