TL;DR
A random forest is a crowd of decision trees. Each tree is grown on a random resample of the training data, and at every split it may consider only a random subset of the features. The forest predicts by averaging the trees (for numbers) or taking a majority vote (for categories).
Single deep trees are flexible but unstable: they chase noise. Averaging many of them (Ensemble Learning) keeps the flexibility and removes much of the instability, and the random choices keep the trees from all making the same mistake.
Where you'll see it: as a strong default for tabular data (rows of examples, columns of measured features, such as customer records and sensor logs), as a quick baseline before reaching for heavier models, and as a way to rank which input features matter most.
Intuition
Imagine a hard question, such as whether a loan will default, and a room of analysts. Each analyst has seen a different random sample of past loans, and each one is allowed to look at only a random handful of the available details before making a decision. Any single analyst is noisy. But if you average their answers, the individual quirks mostly cancel out, and what remains is the signal they all share.
A single decision tree is one such analyst. Left to grow, it keeps asking questions until every training example sits in its own leaf, so it memorises the training set, noise included (Overfitting). That makes it low in bias but high in variance (the Bias-Variance Tradeoff; variance is explained in Mean and Variance): change the training data slightly and the tree changes a lot.
Bagging (Bagging) fixes part of this by training each tree on a bootstrap resample (Bootstrap Sampling) and averaging. But there is a catch. If one feature is very strong, every tree will split on it first, and the trees become similar. Averaging similar trees removes little variance. The random forest adds a second source of randomness, the feature subset at each split, so the trees differ more from one another.
- trees correlated at 0.5
- trees correlated at 0.1
The math
The bootstrap sample
Given training examples, the tree is trained on a bootstrap sample of size : draw examples with replacement. Each example has probability of not being drawn on a single draw, so the chance it is never drawn in all draws is . As grows this approaches . So each tree sees about 63 percent of the distinct training examples, and about 37 percent are left out. Those left-out examples are called out-of-bag for that tree.
Random feature selection
At each node of a tree, the algorithm draws of the features at random and searches only among them for the best split. A common default is for classification and for regression.
For classification the split quality is usually measured with Gini impurity. If a node holds a fraction of class , its impurity is
which is 0 for a pure node and largest when the classes are evenly mixed. The tree chooses the split that most reduces the weighted impurity of its children. (Entropy, see Entropy, can be used instead; the idea is the same.)
Aggregation
Let be the prediction of tree for input . With trees, the forest predicts
for regression, and the majority class (or the average of predicted class probabilities) for classification.
Why averaging helps: the variance formula
Suppose each tree's prediction at a fixed input has variance , and every pair of trees has correlation (a number from 0, unrelated, to 1, identical). The variance of the average is
There are variance terms, each , and covariance terms, each . So
The first term, , does not shrink as grows. It is the floor. Only the second term, which is the part due to each tree's own randomness, falls with more trees. Lowering is therefore the lever that matters. That is exactly what the random feature selection does.
Out-of-bag error
Each example is out-of-bag for about 37 percent of the trees. Predict each training example using only the trees that did not see it, then compare with the true label. The result is an honest error estimate with no separate validation set, which is useful when data is scarce (see Train, Validation and Test Sets).
Worked examples
Suppose one tree's prediction error at a fixed input has variance , and any two trees have correlation . A forest with trees has variance
- Plug into the formula: .
- Compute the bracket: .
- Multiply: .
Interpretation: a single tree has variance 4, and the three-tree forest has about 2.67. Two-thirds of the single-tree variance survives, because the trees' shared errors cannot be averaged away. With large, the variance approaches , and the rest of the reduction is limited by how correlated the trees are.
A node holds 10 examples: 6 spam and 4 not spam. Two candidate splits are available.
Parent: , so .
Split A sends 5 examples left (4 spam, 1 not) and 5 right (2 spam, 2 not).
- Left: , so .
- Right: , so .
- Weighted impurity: . Gain: .
Split B sends 6 examples left (5 spam, 1 not) and 4 right (1 spam, 3 not).
- Left: , so .
- Right: , so .
- Weighted impurity: . Gain: .
Interpretation: split B removes more impurity, so a tree that considers both splits picks B. If the random feature subset at that node contains only the feature for split A, the tree must use A, which is the randomness the forest relies on.
A forest has 500 trees on a dataset with features. Use , rounded to 4 features per split.
- Chance a given feature is among the 4 candidates at a split: . So a strong feature is hidden from 80 percent of the splits, and different trees take different routes to the same answer.
- Out-of-bag coverage: each example is out-of-bag for about trees, which is enough to form a stable out-of-bag prediction for it.
- Feature importance by permutation: shuffle one feature's column in the out-of-bag data and measure how much the accuracy drops. A large drop means the forest relies on that feature.
The counts here are simple arithmetic for this setup, not measurements. They show how each design choice touches the result: fewer candidate features mean more diverse trees, and out-of-bag predictions cost nothing extra.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Data is images, audio or text with rich structure | Trees split on raw pixels or words poorly, and forests lose to learned features. | Use a Feedforward Neural Network or a model suited to the structure. |
| You need calibrated probabilities | Calibrated means a predicted 0.8 really comes true about 80 percent of the time. Averaged votes from many trees rarely come out near 0 or 1, so the probabilities tend to be pulled toward the middle. | Apply a calibration step, or use a model trained to output probabilities directly. |
| The model must extrapolate beyond the training range | Trees predict a constant in each leaf, so they cannot go past the largest value seen. | Use a linear model, or a method that extrapolates. |
| Prediction must be very fast with a tight memory budget | Hundreds of deep trees take time and memory. | Limit tree depth, reduce the number of trees, or train one small model to imitate the forest's outputs. |
| Features are very many and mostly noise | Random feature subsets often contain only noise, so each tree is weak. | Remove irrelevant features first, or increase . |
Random forests are rarely the best model on a dataset they were not designed for, but they are hard to beat as a reliable first try: they need little tuning and seldom overfit badly.
History
Tin Kam Ho introduced random decision forests in 1995, using random subspaces of the features to grow each tree. Yali Amit and Donald Geman had used a related randomised-tree idea for shape recognition in 1997. Leo Breiman brought the pieces together in 2001 in the paper "Random Forests", building on his earlier bagging method from 1996. Breiman's version combined bootstrap resampling with random feature selection at each split, and added out-of-bag error and variable importance. It caught on because it was accurate, needed almost no tuning, and was easy to parallelise, since trees are independent of one another.
Recap
A single deep Decision Tree has low bias but high variance. Bagging averages trees trained on bootstrap resamples, but strong features make those trees alike. A random forest also limits each split to a random feature subset, which lowers the correlation between trees and so the floor on the forest's variance. Out-of-bag predictions give a free error estimate. From here, boosting builds trees one after another to reduce bias instead of variance.
Check yourself
QuestionWhat two sources of randomness does a random forest use, and what does each one add?Show answer
The first is the bootstrap resample: each tree trains on a sample drawn with replacement, so the trees see different data. The second is the random feature subset at each split: each split searches only a few features, so the trees differ in structure. Together they decorrelate the trees, which is what lets averaging reduce variance further than bagging alone.
QuestionA node has 8 examples: 4 of class A and 4 of class B. What is its Gini impurity, and what is the impurity of a pure node?Show answer
For the node, , so . A pure node has one class with and the other with , so . The impurity is largest when the classes are evenly mixed.
QuestionWhy does the variance of a forest of trees stop falling as grows, and what controls where it levels off?Show answer
The variance is . As grows, the term goes to zero, but remains. The level it approaches is set by the correlation between trees. Making trees less correlated, for example by using a smaller feature subset at each split, lowers that floor.
QuestionA forest of 300 trees is trained. Roughly how many trees leave a given training example out, and how can that count be used?Show answer
About trees leave the example out of their bootstrap sample. Those trees form its out-of-bag prediction, which is an honest estimate of error. Averaging the out-of-bag errors over all examples gives a validation-style score without holding out a separate test set.