03 · Supervised · 5 min read · updated
How does a random forest work and why is it better than a single tree?
In short
A random forest averages many trees trained on random samples and feature subsets. On 569 breast tumours one tree scores 90.2%, a forest of 500 trees 94.4%.
What it is
A random forest is an ensemble of many decision trees, each trained on a different random sample of the data and choosing, at every split, from a random subset of features. The forest's prediction is the majority vote of the trees (classification) or the average of their predictions (regression). The method in its current form was described by Leo Breiman in 2001.
A single deep tree is like an expert with a perfect memory and poor judgement: it remembers every training case, but its rules change dramatically with a small change in the data. A forest is a council of hundreds of such experts, each of whom saw slightly different data and looked at slightly different features. Their individual quirks cancel out and the shared signal remains.
For two decades the random forest has been one of the safest choices for tabular data: it works well without tuning, does not need feature scaling and rarely fails spectacularly.
Mechanism — why it works this way
The forest combines two sources of randomness. The first is bagging: each tree gets a bootstrap sample — as many examples as the dataset has, but drawn with replacement, so some repeat and about 37% do not make it into the sample at all. The second is feature sampling: at each split the tree considers only m randomly chosen features instead of all of them (for classification typically m ≈ √p).
Why is the second source needed? Averaging reduces variance only when the trees make different errors. The variance of the average of B trees, each with variance σ² and pairwise correlation ρ, is ρσ² + (1 − ρ)σ²/B. The second term vanishes for large B, but the first remains — the correlation between trees sets a floor the forest cannot go below. If one feature is very strong, all trees built by bagging alone will start with it and end up similar. Feature sampling forces some trees to find other routes, which lowers ρ.
Trees in a forest are usually grown without pruning. Each one alone overfits (low bias, high variance), but averaging removes the variance and leaves the low bias. That is why adding trees does not cause overfitting — the score stabilises and the only cost is computation time.
A bonus from bootstrapping: each example was left out of about 37% of the trees, so it can be evaluated using only those trees. This out-of-bag (OOB) error is a free approximation of cross-validation.
Limitations: a forest does not extrapolate (in regression it cannot predict values outside the training range), loses the readability of a single tree and can be large in memory. The built-in feature importance (mean decrease in impurity) favours continuous and high-cardinality features; permutation importance is more reliable.
By example
We took 569 breast tumours described by 30 measurements and checked how often a model correctly tells a malignant tumour from a benign one on cases it did not see in training. A single fully grown tree was right in 90.2% of cases, a forest of 500 trees in 94.4%. A forest can also estimate its own accuracy without holding out any data: each tree is checked on the tumours it did not draw for training (the so-called OOB estimate). When the comparison is repeated many times on different splits of the data, the order stays the same: tree 92.5%, forest 96.0%.
Why the advantage? At every question each tree in the forest sees only a few randomly chosen features, so the trees make less correlated mistakes, and their errors partly cancel out in the vote. Five trees already do much better than one, and from 50 trees onward the score stops changing. The features that matter most describe the size and irregularity of the tumour.
How it was computed
- Data: Breast Cancer Wisconsin (569 tumours, 30 features); stratified split: 426 training, 143 test,
random_state=0. - One tree: fully grown
DecisionTreeClassifier(random_state=0)— 90.2% on test; with 20 differentrandom_statevalues anywhere from 89.5% to 93.0%. - Forest:
RandomForestClassifier, 500 trees, √30 ≈ 5 features per split by default,random_state=0— 94.4% on test, OOB estimate 96.5%. - Repeated stratified cross-validation: 5 folds × 10 repeats,
random_state=0— tree 92.5%, bagging with 100 trees 95.5%, random forest of 500 trees 96.0%. - Decorrelation: average correlation of test-set predicted probabilities between pairs of trees (a forest of 500 trees) — 0.83 with bagging alone (all 30 features at every split), 0.79 when sampling √30 ≈ 5 features.
- Number of trees (test): 1 tree 88.1%, 5 trees 95.8%, from 50 trees onward 94.4%; the small fluctuations with only a few trees are the noise of 143 test cases.
- Built-in feature importance: worst perimeter 0.15, worst number of concave points of the contour 0.12, worst radius 0.12.
In practice
RandomForestClassifier(n_estimators=500, n_jobs=-1, random_state=0)andRandomForestRegressor; no feature scaling needed.n_estimators: more means more stable (typically 300–1000); it does not cause overfitting, it only costs time.max_featuresis the main parameter:"sqrt"by default for classification and1.0(all features) for regression; values of 0.3–0.5 are worth trying.oob_score=Truegives a free accuracy estimate; for feature importance usepermutation_importanceon validation data.min_samples_leafof 1–5 smooths predictions and shrinks the model;max_depthis usually left unlimited.
Frequently asked questions
- Can a random forest overfit?
- Adding trees does not make a forest overfit — the error stabilises at a level set by the correlation between trees. A forest can, however, fit noise through overly deep trees on very noisy data; a larger `min_samples_leaf` helps there. 100% training accuracy is normal for a forest and does not indicate a problem.
- Random forest or gradient boosting?
- Gradient boosting (XGBoost, LightGBM) usually achieves slightly better results once tuned, but is more sensitive to hyperparameters. The forest is more robust and good out of the box. In practice the forest is an excellent first model, and boosting is the tool for squeezing out the last few percentage points.
- How do you interpret a random forest?
- You can no longer read a single tree, but you can probe the model from the outside: permutation importance shows which features are needed, partial dependence plots show how the prediction changes with a feature's value, and SHAP values decompose an individual prediction into feature contributions.
Sources
- Breiman L. "Random Forests", Machine Learning 45(1), 2001.
- Hastie T., Tibshirani R., Friedman J. "The Elements of Statistical Learning", 2nd ed., 2009, ch. 15.
- James G., Witten D., Hastie T., Tibshirani R. "An Introduction to Statistical Learning", 2nd ed., 2021, ch. 8.2.2.
- Strobl C., Boulesteix A.-L., Zeileis A., Hothorn T. "Bias in Random Forest Variable Importance Measures", BMC Bioinformatics 8, 2007.
- scikit-learn documentation, "Forests of randomized trees": https://scikit-learn.org/stable/modules/ensemble.html#forest
Was this helpful?
Thanks! This helps us improve the Atlas.