Decision Trees and Information Gain
A decision tree is a graph of yes/no questions that ends in predictions. It's built greedily: at each node, ask the question whose answer most reduces the uncertainty (the entropy) of the labels, and stop before the tree just memorizes the data.
What the symbols mean
- Entropy
How mixed the labels in set S are, in bits: 0 when all agree, 1 for a 50/50 split of two classes.
- Class fraction
The fraction of examples in S with label c.
What the symbols mean
- Information gain
How much a question lowers the entropy, on average over its answers.
- Child set
The examples that go down branch k.
- Branch weight
The fraction of examples that take branch k.
What the symbols mean
- Gini impurity
The chance two examples drawn at random (with replacement) from S have different labels.
Explanation
A decision tree is simply a flowchart of yes/no questions that ends in a prediction: "Is the vibration level above 5.5? If yes, predict the machine will fail. If no, is the temperature above 70?" and so on. As a graph, it's a rooted tree from Trees: each internal node is a question, each edge is an answer, and each leaf is a prediction. Making a prediction means walking one path from the root to a leaf. Building the tree is the interesting part, and it comes down to one formula: the information gain , which is just "how mixed were the labels before the question" minus "how mixed are they after, on average." Pick the question that unmixes them the most. That's the whole algorithm.
Entropy measures the mix. From Entropy and KL Divergence, the entropy is the average surprise of a label, in bits. A node where every example has the same label has : nothing to learn. A 50/50 node of two classes has bit, the most uncertain two classes can be. A node with 60% "failed" and 40% "fine" has
almost as mixed as a coin flip.
Information gain scores a question. A question splits the examples at a node into children . Each child has its own entropy, and we average them, weighting each by the fraction of examples that land there. The drop from the parent's entropy is the information gain: on average, how many bits the answer tells you about the label. The weighting matters. A question that peels off one example into a perfectly pure child and leaves everything else as mixed as before has gained almost nothing.
Growing the tree greedily. At the root, try every candidate question and keep the one with the biggest gain. Then repeat in each child, with only that child's examples, until the nodes are pure or some stopping rule kicks in. For a numeric feature, the candidates are thresholds: sort the values and try the midpoints between neighbors, since any threshold between the same two values splits the data the same way. This is greedy, not optimal. Finding the smallest tree that fits the data is a far harder problem, and greedy splitting works well in practice.
Gini impurity. Many libraries use instead of entropy. It's the chance that two examples drawn at random (with replacement) disagree, and it behaves very much like entropy (0 for a pure node, largest for an even mix) while skipping the logarithms. The two usually pick the same splits.
Why trees overfit, and what fixes it. Keep splitting and every leaf eventually holds a single training example: zero training error and a tree that has memorized the noise. Trees are limited by maximum depth, a minimum number of examples per leaf, or pruning back branches that don't help on held-out data (the trade-off from Overfitting, Bias–Variance and Regularization). A random forest trains hundreds of trees on random resamples of the data and averages their votes, which cancels much of each tree's variance. Gradient-boosted trees add small trees one at a time, each correcting the previous ones' errors, and they win most competitions on tabular data.
Trees as graphs. A few facts from discrete math pay off here. A binary tree of depth has at most leaves, so separating classes needs depth at least . A tree where every question has two answers and leaves has exactly questions. And a prediction costs one comparison per level, which is why trees answer in microseconds.
A common mistake. It's tempting to pick the split that produces the purest child. Look at the threshold animation further down: splitting the machines at 3.5 makes a perfectly pure group of 3, but the other 7 stay mixed, and the gain is 0.557 bits. Splitting at 5.5 leaves one child slightly mixed but cleans up both sides more overall, for a gain of 0.610. Always weight each child's entropy by its size.
See it
Worked examples
Ten machines have vibration levels . The ones at failed within a month; the rest are still running. Find the information gain of the question "vibration ?"
- 1
Start with the parent: 6 failed and 4 running out of 10, so and .
Compare the same two splits (at and at ) using Gini impurity instead of entropy.
- 1
Parent Gini: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A node holds 8 positive and 8 negative examples. What is its entropy, in bits?
A node holds 3 positive and 1 negative example. What is its entropy, in bits, to 4 decimal places?
What is the Gini impurity of a node with 3 positive and 1 negative example?
A node of 10 examples is 5 yes and 5 no. A question splits it into a left child of 4 (all yes) and a right child of 6 (1 yes, 5 no). What is the information gain, to 4 decimal places?
A decision tree asks only yes/no questions and has 12 leaves. How many questions (internal nodes) does it have?
A mail filter has 100 emails, 40 of them spam. The question "contains a link?" splits them into 50 with links (35 spam) and 50 without (5 spam). What is the information gain of this question, in bits, to 4 decimal places?
A tree grown until every leaf is pure gets 100% accuracy on its training data but does poorly on new data. What's going on?
A numeric feature takes the values in the training data at a node. How many distinct candidate thresholds does a tree need to try?
The hardest one here. Get it, and you've got this lesson.
A node has 12 examples, 4 each of classes A, B and C. Split X sends (4 A, 4 B) left and (4 C) right. Split Y sends (4 A, 2 B) left and (2 B, 4 C) right. Which split does the tree choose, and what is its information gain? Give the gain in bits to 4 decimal places.
Where it's used
Decision trees, and the random forests and gradient-boosted ensembles built from them, are the workhorses for tabular data: credit scoring, insurance pricing, predicting which customers will cancel, and flagging machines for maintenance from sensor readings. Hospitals like single small trees for triage rules because a doctor can read every question. Boosted trees train in minutes on a laptop and regularly beat deep networks on spreadsheet-style data.