Play 20 Questions well and you'll notice a pattern in the best players: their first question is never "is it a rock, specifically a piece of granite, from a quarry in Wales?" It's something broad like "is it alive?" Why? Because "is it alive?" roughly splits the world in half — whichever answer comes back, you've thrown away about half of all possible objects. "Is it a rock specifically?" almost certainly gets a "no" and barely narrows anything down at all.
A decision tree faces exactly the same problem at every single node: out of all the questions it could ask about the data, which one should it ask first? To answer that, it needs a way to measure two things — how "mixed up" a group of examples currently is (entropy), and how much a candidate question would clean that mixture up (information gain). The tree then simply asks whichever question gains the most.
This isn't just a cute analogy — it's the exact same arithmetic. A tree trying to predict whether a customer will buy a product doesn't know in advance whether "age over 30?" or "owns a pet?" is the smarter opening question. It has to try both, measure how much each one would clean up the mixed group of buyers and non-buyers, and pick the winner — precisely the way a sharp 20-Questions player picks "is it alive?" over "is it a rock, specifically granite?"
Imagine a bag of labelled examples — say, "will this customer buy the product? yes or no." If
every example in the bag is "yes," there's no mystery left: reach in and you already know
what you'll pull out. That group is perfectly pure, and we say its entropy is
For a group split into two classes with proportions
Check it matches the story at the extremes: at
(This page sticks to two classes — "yes/no," "spam/not spam" — because that's what most splits boil
down to. The same idea stretches to more classes by adding one
Slide the class mix below and watch the curve. Entropy climbs from
Entropy alone tells you how mixed up one group is. To pick a splitting question, a tree needs to know how much a candidate split would clean things up — that's information gain: the parent group's entropy, minus the average entropy of the children the split creates (each child's entropy weighted by how large it is):
At every node, the tree tries every candidate question, computes the information gain each one would give, and greedily picks the largest one. Do that node after node, and a whole tree grows itself, one locally-best question at a time. (Some tree libraries use a near-identical measure called Gini impurity instead of entropy — the spirit, and the greedy "pick the biggest improvement" logic, is the same either way.)
Notice the word greedily. The tree never looks two questions ahead to check whether a slightly worse first question might set up a much better second one — it just takes whichever gain is biggest right now, at every node, forever. That's usually a very good strategy (and a fast one — checking every possible pair of questions in advance would be far too slow), but it's not guaranteed to build the absolute best possible tree. It's also exactly the habit that gets a tree into trouble, as the next card shows.
Suppose a node holds
Compare that to the two extremes: a group that's all 8 "yes" would score
Take that same 8-customer group (entropy
Split A — "Age over 30?" makes two children of 4 each: the "over 30" child is
4 yes / 0 no (entropy
Split B — "Owns a pet?" makes a child of 5 (3 yes / 2 no, entropy
The two worked examples above are just a handful of multiplications and logarithms. Run the code below to check the numbers land exactly where the by-hand working said they would — and try changing the group sizes to see the gains shift.
Put a number on it: split our 8-customer group by ID number instead of age or
pets, and you get 8 children of size 1 — each one either 100% "yes" or 100% "no," so every single
child has entropy
This formula isn't a machine-learning invention at all — it's borrowed wholesale from
It also explains why a sharp 20-Questions player can nail an object out of over a million
possibilities in just 20 yes/no questions. Each perfectly-chosen question should roughly halve the
remaining possibilities, and
Shannon himself worked at Bell Labs, and legend has it he liked to ride a unicycle down the corridors while juggling — a fitting image for someone who could make something as slippery as "information" as precise and countable as a length in metres or a mass in kilograms.
Next: a tree that greedily chases information gain, split after split, will happily fall straight
into that ID-column trap and others like it — carving out ever-tinier, ever-purer groups that fit
the training data perfectly but generalize terribly. That's exactly the story of