Random forest or gradient boosting: what does each one fix?
Question
Your team is choosing between a random forest and gradient boosted trees for a churn model. A colleague says: "Both are just many trees, so they behave the same."
Explain how each one builds and combines its trees. Which error does each lower: bias or variance? When does each overfit? Which settings matter most?
Show hints (3)
- "HOW TREES COMBINE"
A random forest trains its trees separately and averages them. Boosting trains trees one after another. What does each new boosting tree learn from?
- "WHAT AVERAGING CAN'T DO"
Averaging many noisy guesses makes them steadier. Can averaging many models that are too simple make the result less simple?
- "MORE TREES"
More forest trees average more of the same kind of tree. What is left for new boosting trees to fit once the signal is learned?
Show checked explanation
A random forest averages many deep trees, each trained on a random sample of rows with random features tried at each split. Averaging lowers variance. Boosting adds small trees one at a time, each fitting the errors left so far, so it lowers bias. More forest trees don't overfit. Too many boosting rounds do.
import numpy as np from sklearn.ensemble import GradientBoostingRegressor as Boosting from sklearn.ensemble import RandomForestRegressor as Forest from sklearn.metrics import mean_squared_error rng = np.random.default_rng(0) X = np.arange(40).reshape(-1, 1) / 2 # 0, 0.5, ..., 19.5 y = 10 * X.ravel() + rng.normal(0, 15, 40).round() # a noisy line X_test = X + 0.25 y_test = 10 * X_test.ravel() # the true line models = {f"forest, deep trees, n={n}": Forest(n, random_state=0) for n in [1, 10, 1000]} models["forest, stumps, n=100"] = Forest(100, max_depth=1, random_state=0) for n in [10, 100, 3000]: models[f"boosting, stumps, n={n}"] = Boosting( n_estimators=n, max_depth=1, random_state=0) for name, model in models.items(): error = mean_squared_error(y_test, model.fit(X, y).predict(X_test)) print(f"{name}: test error {error:.1f}")
forest, deep trees, n=1: test error 143.4 forest, deep trees, n=10: test error 81.1 forest, deep trees, n=1000: test error 86.1 forest, stumps, n=100: test error 707.4 boosting, stumps, n=10: test error 858.2 boosting, stumps, n=100: test error 75.7 boosting, stumps, n=3000: test error 113.2
Code run on CPython 3.12 with scikit-learn 1.9 against the example above. Checked by an editor other than the author.
Walkthrough
The code fits a noisy line of 40 points. Test error is measured against the true line. Other random noise gives other numbers, but the same pattern:
| model | test error |
|---|---|
| forest, 1 deep tree | 143.4 |
| forest, 10 deep trees | 81.1 |
| forest, 1000 deep trees | 86.1 |
| forest, 100 stumps | 707.4 |
| boosting, 10 stumps | 858.2 |
| boosting, 100 stumps | 75.7 |
| boosting, 3000 stumps | 113.2 |
A random forest is bagging plus random features. Each tree grows deep on a bootstrap sample (rows drawn at random, with repeats). A deep tree has low bias but high variance: it follows the noise in its sample. Different samples give different mistakes, and the average cancels many of them. One deep tree scores 143.4, ten score 81.1. After that the error levels off.
Averaging can't fix bias. A stump is a tree with one split. The average of 100 stumps is still a crude model: 707.4.
Boosting goes the other way. It starts from weak, high-bias trees such as stumps. Each new tree fits the errors the model still makes and is added with a small weight, the learning rate. 10 stumps underfit (858.2). 100 stumps fit the line well (75.7). Later trees fit the noise, and 3000 stumps do worse (113.2).
The settings that matter most:
- Random forest: tree depth or minimum leaf size, and the features tried per split. Fewer features make the trees less alike: variance drops, bias rises a little. More trees are safe, only slower.
- Boosting: rounds and learning rate, tuned together. Small trees (scikit-learn's default depth is 3). Early stopping. A
subsamplebelow 1 trains each tree on part of the rows.
Common mistakes
- Saying boosting can't overfit. Once the signal is learned, the remaining error is noise, and new trees fit it too. Stop early on validation error (
n_iter_no_changein scikit-learn). - Lowering the learning rate without adding rounds. Each tree then fixes less of the error, so the same number of rounds underfits.
- Adding forest trees to fight overfitting. More trees only make the average steadier. Limit the depth or raise the minimum leaf size instead.
- Keeping all features at every split. The trees look alike, and averaging helps less. Scikit-learn's forest regressor does this by default; the classifier tries the square root of the feature count.
Follow-up questions
- "Why does boosting use small trees and a small learning rate?" Each small tree adds a little, and the learning rate shrinks it further. With small steps, validation error shows where the model starts to fit noise, and you stop there. Big trees with a big learning rate get there within a few rounds.
- "Which one would you try first on tabular data?" A random forest makes a good baseline: it works well with default settings. Tuned gradient boosting can score higher, but it needs care with rounds, learning rate and early stopping. Try both and compare them on the same validation data.
What a strong answer covers
Explains bagging as averaging deep, different trees to cut variance, and boosting as adding small trees on the remaining error to cut bias. Knows more forest trees are safe but more boosting rounds can overfit. Names the key settings for each.
Open in interactive practice deck →