Decision Trees
Machine learning you can actually read. A tree of simple yes-or-no questions that classifies data, chosen by measuring how much each question reduces uncertainty.
Last week's clustering learned with no labels. Now we move to supervised learning, where the data comes with the right answers and the model learns to predict them. Decision trees are the friendliest place to start, because unlike most models you can read them. A decision tree is just a flowchart of yes-or-no questions ending in an answer. The only real cleverness is in how the tree decides which question to ask first, and that decision comes down to a single beautiful idea: pick the question that reduces uncertainty the most.
Trees you can read
A decision tree is a flowchart that makes a prediction. You start at the top and answer a question about the data; the answer sends you down a branch to another question, and so on, until you reach a leaf that gives the final answer, the predicted class.
- Each internal node asks a question about one feature, for example "is the outlook sunny?"
- Each branch is a possible answer.
- Each leaf is a decision, the class the tree predicts for anything that reaches it.
The great appeal is transparency. Many machine learning models are black boxes, but you can print a decision tree and follow exactly why it made any prediction. That readability makes trees popular wherever a decision has to be explained, like credit approvals or medical triage.
Entropy: measuring uncertainty
To choose a good question, we need to measure how mixed up a set of examples is. If a group is all one class, it is pure and certain. If it is a fifty-fifty mix, it is as uncertain as possible. Entropy is the number that captures this, from 0 (perfectly pure) to 1 (maximally mixed, for two classes).
- All examples the same class: entropy 0, no uncertainty.
- Half and half (two classes): entropy 1, maximum uncertainty.
- Anything in between: entropy somewhere between 0 and 1.
Information gain
Now the key idea. When we split a group of examples using some question, we create smaller subgroups. Information gain measures how much the split reduced entropy: the uncertainty before, minus the (weighted) uncertainty after.
The subgroups are weighted by their size, so a split that produces one big pure group and one small messy one still scores well. To build the tree, the algorithm simply tries every possible question and picks the one with the highest information gain. That question becomes the node, and the process repeats on each subgroup.
Building a tree with ID3
The classic algorithm that does this is ID3. It is refreshingly simple: at each node, choose the feature with the highest information gain, split on it, and recurse on each branch until the groups are pure or you run out of features.
A dataset of 14 days, labelled "play tennis" yes or no: 9 yes, 5 no. First the entropy before any split:
A gain of 0.048 is small, meaning Wind is a weak question. ID3 would compute the gain of every feature and choose the one with the highest gain (for this classic dataset, Outlook wins) as the root.
Wind information gain = 0.048, a poor splitFind the best split
Numbers on a page are abstract, so try it directly. Below are points of two classes, red and blue. Choose which feature to split on (the horizontal or vertical axis), then drag the split line. The panel shows the entropy before, the weighted entropy after, and the information gain. Hunt for the split with the highest gain, then notice which axis can even produce a good one.
Red and blue are two classes. The split line divides the points into two groups.
Overfitting and forests
Left unchecked, a decision tree will keep splitting until every leaf is perfectly pure, memorising the training data down to its noise. This is overfitting: the tree scores perfectly on data it has seen and poorly on anything new, because it learned the quirks of the sample rather than the real pattern.
Two cures:
- Pruning: cut the tree back, removing branches that add little real value, so it stays simpler and generalises better. You can stop growing early or trim after the fact.
- Random forests: instead of one tree, train many trees on random slices of the data and features, and let them vote. No single tree overfits the same way, so the crowd is far more robust than any individual. This is one of the most reliable machine learning methods there is.
Key points and practice
- A decision tree classifies by asking a chain of questions ending at a leaf; it is readable.
- Entropy measures how mixed a group is, from 0 (pure) to 1 (evenly split).
- Information gain is the drop in entropy from a split; the tree picks the split with the most gain.
- ID3 builds the tree by choosing the highest-gain feature at each node and recursing.
- The Gini index is a common alternative impurity measure to entropy.
- Trees overfit if grown fully; pruning and random forests fix this.
Entropy measures how mixed a set of examples is with respect to their class labels, in other words how much uncertainty there is about a randomly chosen example's class. An entropy of 0 means the set is pure, all examples belong to one class, so there is no uncertainty. For two classes, an entropy of 1 means the set is split evenly between them, the maximum possible uncertainty.
ID3 computes the entropy of the current set of examples, then for each feature it works out the weighted entropy that would result from splitting on that feature, weighting each subgroup by its size. The information gain of a feature is the entropy before the split minus this weighted entropy after. ID3 selects the feature with the highest information gain, because that split reduces uncertainty the most, makes it the node, and repeats the process on each resulting branch.
Overfitting is when a tree grows so detailed that it memorises the training data, including its noise, and therefore performs well on that data but poorly on new, unseen data. It can be reduced by pruning, cutting back branches that add little value so the tree stays simpler and generalises better, or by using a random forest, which trains many trees on random subsets of data and features and lets them vote, making the overall prediction far more robust.
