What is a random forest?
A random forest is an ensemble of decision trees, each trained on a bootstrap sample of the data and limited to a random subset of features at every split. For classification the forest takes a majority vote of its trees, and for regression it averages their predictions. Training each tree on a resampled copy of the data is called bagging, short for bootstrap aggregating. The random feature subsets are what turn bagged trees into a random forest.
Random forests are a default choice for tabular data because they work well with little tuning, handle mixed feature types, and do not overfit as more trees are added. They are used in credit risk, fraud detection, ecology, genomics, and remote sensing. They also come with two useful by-products: an out-of-bag estimate of test error and feature importance scores.
Key ideas
- Bagging trains each model on a bootstrap sample drawn with replacement and combines their predictions.
- A bootstrap sample contains about 63.2% of the unique training points.
- Averaging many high-variance trees reduces variance without adding much bias.
- Random forests also limit each split to a random subset of features, which decorrelates the trees.
- Out-of-bag points give a built-in estimate of test error.
- Adding more trees does not cause overfitting; it only stabilizes the ensemble.
Common questions
What is the difference between bagging and a random forest?
Bagging trains every tree on a bootstrap sample but lets each split consider all features. A random forest adds a second source of randomness, a random subset of features at every split. That makes the trees less correlated, so averaging them reduces variance further.
How many trees should a random forest have?
Enough that the error stops improving, often a few hundred. More trees never make the forest overfit, but the gains shrink while training and prediction get slower. Plotting OOB error against the number of trees shows where it levels off.
Is a random forest better than gradient boosting?
Well-tuned gradient boosting usually reaches higher accuracy on tabular data, because it reduces bias by fitting each tree to the errors of the earlier ones. Random forests are easier to tune, harder to overfit, and train their trees in parallel. A common workflow starts with a random forest as a baseline and moves to boosting for the last few points of accuracy.
Can a random forest overfit?
Its error does not rise as trees are added, but a forest can still overfit noisy data when its trees grow very deep on few samples. Limiting tree depth or raising the minimum number of samples per leaf helps in that case. Checking the OOB error is a quick way to spot it.
Related explainers
- Decision Trees & Splitting — Recursive partitioning, Gini impurity, and pruning
- Gradient Boosting — Sequential residual fitting for powerful predictions
- Ensemble Methods & Stacking — Voting, stacking, and combining diverse models
- Bias-Variance Tradeoff — Why models underfit or overfit — the fundamental decomposition
- Overfitting & Underfitting — The bias-variance tradeoff and generalization
- Cross-Validation — K-fold, stratified, and leave-one-out evaluation
References
- Breiman (2001), "Random Forests", Machine Learning
- Breiman (1996), "Bagging Predictors", Machine Learning
- James, Witten, Hastie & Tibshirani, "An Introduction to Statistical Learning", chapter 8: Tree-Based Methods
Last updated