A flowchart learned from data
A decision tree is a flowchart built from examples. Each internal node tests one feature, such as whether age is above 30. Each branch is an outcome of the test, and each leaf holds a prediction.
A tree that predicts a category is a classification tree. A tree that predicts a number is a regression tree, and its leaves usually hold the mean of the training targets that reached them.
How the tree chooses its splits
Training starts with all examples at the root. The algorithm tries candidate splits and keeps the one that makes the groups purest. Purity is often measured by Gini impurity, which is 1 minus the sum of squared class proportions. Entropy is another common measure.
The same step repeats on each child group. This is called recursive splitting. It stops when a group is pure, when a depth limit is reached, or when too few examples remain.
Worked example: scoring one split with Gini
Take 10 emails, 5 spam and 5 not spam. Gini at the root is 1 - (0.5² + 0.5²) = 1 - 0.5 = 0.5.
Suppose a split on the word "offer" sends 4 emails left, all spam, and 6 right, with 1 spam and 5 not spam. Left Gini is 0. Right Gini is 1 - ((1/6)² + (5/6)²) = 1 - (1/36 + 25/36) = 10/36, about 0.278.
Weighted Gini after the split is 0.4 × 0 + 0.6 × 0.278 = 0.167. Impurity falls by 0.5 - 0.167 = 0.333, so the split is a good one.
Common misconceptions
Deeper trees are not better by default. A tree grown until every leaf is pure usually memorizes noise, which is called overfitting. Limiting depth or pruning branches usually improves results on new data.
Trees do not need feature scaling, because each test compares one feature to a threshold. They are not automatically stable, though. Small changes in the training data can produce a very different tree.
Where decision trees are used
Trees suit tabular data such as credit scoring, medical triage rules and customer churn. Their paths can be read as if-then rules, which helps when decisions must be explained.
Random forests and gradient boosted trees combine many trees to gain accuracy. They are widely used on tabular data.
Key terms
- Node
- A point in the tree where a feature is tested and the data is divided.
- Leaf
- A final node that holds the prediction and has no further splits.
- Gini impurity
- A measure of how mixed the classes are in a group, equal to 1 minus the sum of squared class proportions.
- Entropy
- An alternative impurity measure based on the logarithm of class proportions.
- Overfitting
- Fitting noise in the training data so that performance on new data is worse.
- Pruning
- Removing branches that add little predictive value, to reduce overfitting.
- Random forest
- An ensemble of many trees trained on varied samples, whose predictions are combined.
