All posts

Decision trees and random forests

/AI/5 min read

A decision tree asks one yes/no question at a time, choosing each by how much purer it makes the groups: splitting 10/10 into 8/2 and 2/8 cuts Gini impurity from 0.5 to 0.32. A forest averages hundreds of deliberately different trees — 100 trees with correlation 0.3 keep only 30.7% of one tree's variance.

A decision tree makes a prediction by asking a sequence of questions about the input, each with a yes or no answer, until it reaches a conclusion. Is income above 50,000? Has the customer missed a payment in the last year? Is the loan longer than five years? Each answer leads to the next question or to a final prediction.

It is the most interpretable model in common use — you can print it and read it — and, combined into random forests and boosted ensembles, still one of the strongest choices for tabular data.

How a tree chooses its questions

Training a tree means choosing, at each point, which question to ask. The tree considers every feature and every threshold, and picks the split that makes the resulting groups as pure as possible — as close as possible to containing only one class.

A common measure of impurity is Gini impurity: the chance that two items picked at random from a group have different labels.

Gini = 1 − Σ pᵢ²

Start with 20 loan applicants, 10 who repaid and 10 who did not:

Gini = 1 − 0.5² − 0.5² = 0.500

Compare two candidate questions:

splitleft groupright groupGini of eachweighted Giniimprovement
"missed a payment?"8 repaid, 2 not2 repaid, 8 not0.3200.3200.180
"owns a car?"6 repaid, 4 not4 repaid, 6 not0.4800.4800.020

The first question separates the groups far better, so the tree asks it. Then it repeats the process inside each group, and inside each of those, until the groups are pure, too small to split, or a depth limit is reached. The prediction at each leaf is the majority class (or the average value, for regression) of the training examples that ended there.

The weakness: one tree overfits

Left to grow, a tree keeps splitting until every leaf is pure — which on real, noisy data means memorising the training set. Its predictions then depend heavily on the particular examples it saw: change a few, and the top split may change, and with it the entire tree below. Deep trees have low bias and high variance.

Pruning and depth limits help, but at the cost of accuracy. The better fix is to stop relying on one tree.

Random forests: many different trees, averaged

A random forest trains hundreds of trees and averages their predictions (or takes a majority vote). Averaging cancels out errors — but only if the trees make different errors. Two sources of randomness make them different:

  1. Bootstrap samples. Each tree trains on a random sample of the data, drawn with replacement, the same size as the original. On average each sample contains about 63.2% of the distinct examples, some repeated, the rest left out.
  2. Random feature subsets. At each split, a tree may choose only among a random subset of the features — often about the square root of the total. This stops every tree from making the same obvious first split.

How much averaging helps depends on how correlated the trees' errors are. If each tree's prediction has variance σ² and any two trees have correlation ρ, the average of B trees has variance:

ρσ² + (1 − ρ)σ² / B

correlation between trees1 tree10 trees100 trees
01.000σ²0.100σ²0.010σ²
0.31.000σ²0.370σ²0.307σ²
0.61.000σ²0.640σ²0.604σ²

Adding trees drives the second term toward zero, but the first term — the shared error — never goes away. That is why the random feature subsets matter: they lower ρ. With correlation 0.3, a hundred trees keep about 31% of a single tree's variance; with 0.6, about 60%, no matter how many trees are added.

ONE TREE, THEN MANY missed payment?10 / 10 · Gini 0.50 noyes 8 repaid / 2Gini 0.32 2 repaid / 8Gini 0.32 gain 0.180 vs 0.020 for "owns a car?" average bootstrap sample + random features each variance of the average = ρσ² + (1 − ρ)σ² / B 100 trees at ρ = 0.3 keep 30.7% of one tree's variance — the shared part never averages out
Each tree splits on purity; the forest's strength comes from how differently its trees err.

A free validation set

Because each tree skips about 36.8% of the data, every training example is left out by roughly a third of the trees. Predicting each example using only those trees gives the out-of-bag error — an honest estimate of generalisation without a separate validation set.

Boosting: the other ensemble

Random forests grow trees independently and average them, which mainly reduces variance. Gradient boosting — XGBoost, LightGBM, CatBoost — grows shallow trees one after another, each fitting the errors the previous ones left, which mainly reduces bias. Boosted trees are often slightly more accurate on tabular data, at the cost of more tuning and more risk of overfitting. Both remain hard to beat for structured, spreadsheet-like data, frequently outperforming neural networks there.

Strengths and limits

Strengths: handle mixed numeric and categorical features, need little preprocessing (no scaling), capture non-linear interactions, are fast, and provide feature-importance estimates.

Limits: a forest is no longer readable like a single tree; trees cannot extrapolate beyond the range of values seen in training; and on images, audio and text, neural networks are far better.

What to take away

A decision tree asks one question at a time, choosing each to maximise purity — "missed a payment?" turns a 10/10 group with Gini 0.5 into two 8/2 groups with Gini 0.32. Grown deep, a single tree overfits.

A random forest averages many trees, each trained on a bootstrap sample (about 63.2% of distinct examples) with random feature choices, so they err differently. The variance left is ρσ² + (1 − ρ)σ²/B: 30.7% of one tree's with 100 trees at correlation 0.3. Lower correlation, not just more trees, is what makes forests strong.