TL;DR
A decision tree is a flowchart learned from data. Each internal node asks a question about one input feature, such as "is the size above 70?", and each leaf gives a prediction. To grow the tree, the algorithm tries many questions and keeps the one that splits the training examples into the purest groups, then repeats on each group.
Trees predict categories (classification) or numbers (regression), and they are easy to read. A single deep tree tends to memorise its training data, so they are usually combined into ensembles.
Where you'll see it: credit and fraud scoring rules, medical triage checklists, and as the base learner inside random forests and gradient-boosted models such as ensembles.
Intuition
A game of twenty questions
Think of the game where one player hides an object and the other asks yes-or-no questions to narrow it down. A good first question removes about half the possibilities. A bad one, like "is it exactly this one thing?", removes almost none. A decision tree plays the same game with data. The "objects" are training examples, labelled with the answer you want to predict, and each question tests one input feature.
Each question sorts the examples into two piles. The best question is the one that leaves each pile as uniform as possible, so that knowing which pile an example is in tells you a lot about its label. Once a pile contains only one label, there is nothing left to ask, and that pile becomes a leaf.
The plot shows why purity is measured the way it is. For a two-class node where a fraction of examples belong to one class, the two standard impurity measures are entropy and Gini impurity. Both are zero when the node is pure and largest when .
- entropy (bits)
- Gini impurity
Two kinds of tree
For classification, a leaf predicts the most common label among its training examples, or the label fractions. For regression, a leaf predicts the average of its training targets. The splitting logic is the same; only the impurity measure changes. The name CART (classification and regression trees) covers both.
flowchart TD R["Sender in contacts?"] -->|"yes"| L1["Leaf: not spam (3 of 3)"] R -->|"no"| N["Contains free?"] N -->|"yes"| L2["Leaf: spam"] N -->|"no"| L3["Leaf: not spam"]
The math
Setup
We have training examples , where is a vector of input features and is the target. A node holds a subset of these examples. A split chooses a feature and a threshold , and sends examples with to a left child and the rest to a right child.
For a node with classes, let be the fraction of belonging to class . The two impurity measures are
Entropy is the measure in the entropy lesson, and it measures average surprise. Gini impurity is the probability that two examples drawn at random from the node carry different labels. Both are zero for a pure node.
Information gain
A split is scored by how much it reduces impurity. With entropy, the gain is
where is the child subset and is the fraction of examples that went there. The weighted average means a split that sends most examples to a very pure child is rewarded. For Gini, replace with ; the formula is otherwise identical.
For regression the impurity is the sum of squared errors around the node mean, , and the gain is the drop in that sum. The leaf prediction is .
Growing the tree
The algorithm is greedy and recursive:
- For each feature, sort the examples by that feature and try a threshold midway between each pair of neighbouring values.
- Keep the feature and threshold with the largest gain.
- Split, and repeat on each child.
- Stop when a node is pure, when it has fewer than a minimum number of examples, or when the depth reaches a limit. Its prediction is then the leaf value.
Greedy means the tree picks the best question now, not the best tree overall. That is cheap and usually good enough, but it is not optimal.
Worked examples
Eight emails: 4 spam, 4 not spam. The root entropy is bit, since the classes are exactly even.
Candidate A, "contains the word free": 4 emails have it (3 spam, 1 not), and 4 do not (1 spam, 3 not).
- Entropy of each child with : .
- Both children have the same entropy, , so the weighted sum is .
- Information gain: bits.
Candidate B, "sender is in contacts": 3 emails are from contacts, all not spam. The other 5 contain 4 spam and 1 not.
- The contacts child is pure, so .
- The other child has : .
- Weighted sum: .
- Information gain: bits.
Interpretation: B removes far more uncertainty than A, so the root asks about contacts first. The contacts branch becomes a leaf, and the other branch is split again.
Four houses, with size in square metres and price in thousands:
| Size | Price |
|---|---|
| 50 | 150 |
| 60 | 170 |
| 80 | 250 |
| 100 | 290 |
- Without any split, predict the mean price . The sum of squared errors is .
- Split at size 70. The left child is with prices 150 and 170, mean 160. Its squared errors are .
- The right child is with prices 250 and 290, mean 270. Its squared errors are .
- Total error after the split is , a reduction of from the root.
- A new 65 square metre house falls on the left, so the tree predicts 160 thousand.
Interpretation: the leaf predicts the average of the houses it contains. A tree with a single split is a step function, so a finer tree gives finer steps and, if it goes too far, starts following noise.
Tradeoffs and failure modes
Trees are easy to explain, need little preprocessing, handle mixed feature types and interactions, and are fast to predict with. Their weaknesses are just as real.
| Situation | Problem | Better choice |
|---|---|---|
| Tree grown until leaves are pure | High variance; memorises noise, see Overfitting | Limit depth or minimum leaf size, or prune |
| Small data changes | Greedy splits can change the whole structure | Bagging or random forests |
| Smooth underlying function | Axis-aligned steps approximate it poorly | Linear models, or boosting many small trees |
| Need a probability, not a label | Leaf fractions are noisy when leaves are small | Calibration, or ensembles that average |
| Very high-cardinality categories | Many possible splits favour features with many values | Target encoding, or limit the feature |
A single tree is usually the wrong final model for accuracy. It is the right model when someone needs to read the rule.
History
The first tree-growing method for data analysis was AID, developed by James Morgan and John Sonquist in the early 1960s to find which variables explained survey responses. Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone published CART in 1984, which gave the cost-complexity pruning and binary splits used in most libraries today. Ross Quinlan developed ID3 in 1986 and C4.5 in 1993, which used information gain and handled categorical features more directly.
Trees became a building block when Breiman showed in 1996 that averaging trees trained on bootstrap samples reduces variance, and in 2001 added random feature selection to form random forests. Those ensembles, not single trees, are where decision trees are most widely used.
Check yourself
QuestionIn one sentence, what makes a split good, and what happens to a node once its examples all share one label?Show answer
A good split leaves the child nodes purer than the parent, meaning lower entropy or Gini impurity, with the largest weighted reduction in impurity. A node whose examples all share one label is pure, so it becomes a leaf that predicts that label.
QuestionA node has 6 examples: 5 of class A and 1 of class B. What is its Gini impurity?Show answer
.
QuestionUsing the spam example, why does information gain for the contacts split (0.549) beat the gain for the word free split (0.189), even though both are binary?Show answer
The gain measures how much the weighted child impurity falls. The contacts split puts three examples into a pure child and five into a child that is 80 percent one class. The free split leaves both children at 75 percent, which removes little uncertainty. A split is rewarded for producing purer children, not for being binary.
QuestionIn the house example, why is the leaf prediction 160 and not the nearest single neighbour's price?Show answer
The leaf predicts the mean of the training targets in its region, because for squared error the mean minimises the sum of squared errors within that leaf. Using one neighbour's price would be a nearest-neighbour rule, and it would give a much noisier prediction.
QuestionYou grow a tree until every leaf is pure and it scores 100 percent on training data but 60 percent on new data. What is happening, and what would you change first?Show answer
The tree has memorised the training examples, including their noise, so its variance is high and it does not generalise. The first change is to limit growth, by setting a maximum depth or a minimum number of examples per leaf, and then to check performance on a validation set. If accuracy is still unstable, averaging many trees with bagging or a random forest is the next step.