Explain ensemble methods — random forests and gradient boosting
Written by the mlprep team · Updated
Explain ensemble methods. How do random forests and gradient boosting work, and why do they often outperform a single strong model?
Ensemble methods combine multiple weak models to produce a stronger one. The two main strategies attack different parts of the bias-variance decomposition.
Bagging — reducing variance (Random Forests)#
Bagging (Bootstrap Aggregating) trains many models independently on random subsets of the data and averages their predictions. Each model is high-variance and low-bias — a deep decision tree is essentially a memorization machine.
Random Forest adds two sources of randomness:
- Each tree is trained on a bootstrap sample (sampled with replacement) of the training data
- At each split, only a random subset of features (typically √d for classification) is considered
These two sources force the trees to be different from each other. When you average predictions across many uncorrelated high-variance models, the errors cancel out. If each tree has error variance σ², n independent trees have average variance σ²/n. The variance is dramatically reduced while bias stays roughly constant.
The key insight: correlation between trees kills the variance reduction. If all trees are similar (because they all use the same strong features), averaging doesn't help much. Random feature selection forces trees to find different paths to prediction, keeping them diverse.
Boosting — reducing bias (Gradient Boosting)#
Boosting builds trees sequentially, where each new tree fits the residuals (errors) of the current ensemble. The algorithm:
- Start with a constant prediction (mean of target)
- Compute residuals: r_i = y_i - ŷ_i
- Fit a new shallow tree to the residuals
- Add this tree to the ensemble with a small learning rate: ŷ ← ŷ + η · tree
- Repeat
Each tree corrects the mistakes of the previous ensemble. You're doing gradient descent in function space — each tree is a step in the direction that most reduces the loss. With squared error loss, the residuals are exactly the negative gradients.
Shallow trees (depth 3-6) are the base learners — each has high bias, but the sequential correction reduces it. Learning rate η (typically 0.01-0.1) controls how aggressively each tree corrects. Smaller learning rate + more trees often generalizes better.
Why tree ensembles dominate tabular data#
Tabular data has heterogeneous features (some continuous, some categorical, some noisy), non-linear interactions, and outliers. Trees naturally handle all of this without feature scaling, are robust to outliers (they split by rank), and capture arbitrary interactions by splitting on multiple features in sequence. Neural networks can learn similar patterns but require more data and careful preprocessing. XGBoost, LightGBM, and CatBoost are the practical defaults for structured data.
You said gradient boosting fits residuals — can you show exactly what happens in one boosting step when the loss is something other than squared error, like log loss?
The general view: gradient descent in function space#
For squared error, the residuals y_i - ŷ_i are exactly the negative gradients of the loss with respect to the predictions:
∂(y_i - ŷ_i)²/∂ŷ_i = -2(y_i - ŷ_i)
"Fitting residuals" is fitting the negative gradient. Gradient boosting generalizes this: for any differentiable loss L, compute the pseudo-residuals (negative gradients) at each step and fit a tree to them.
For log loss (binary cross-entropy)#
With ŷ_i = σ(F_i) (model output passed through sigmoid), the log loss is:
L = -[y_i log(ŷ_i) + (1-y_i) log(1-ŷ_i)]
The negative gradient with respect to the model output F_i is:
r_i = -∂L/∂F_i = y_i - ŷ_i = y_i - σ(F_i)
The pseudo-residual is exactly prediction error — the difference between the true label and the predicted probability. This is elegant: the tree fits the directions where the model is most wrong, regardless of the loss function.
One boosting step#
At iteration t, with current ensemble F(x):
- Compute r_i = y_i - σ(F(x_i)) for all training examples
- Fit a depth-d tree to (x_i, r_i)
- Update: F(x) ← F(x) + η · tree(x)
The tree learns to predict how wrong the current model is for each example, and the ensemble steps in that direction. For examples the model is very confident and wrong about (ŷ ≈ 0 but y = 1), r_i ≈ 1 — a large correction. For well-predicted examples, r_i ≈ 0 — the tree ignores them.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Bagging (Random Forest) trains diverse independent trees and averages them — each tree has high variance, averaging reduces variance while keeping bias low. Boosting (Gradient Boosting) trains trees sequentially, each fitting the negative gradient (pseudo-residuals) of the current ensemble — each step reduces bias. Gradient boosting generalizes to any differentiable loss by always fitting the direction of steepest loss descent.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- What hyperparameters matter most in gradient boosting and how would you tune them?
- How does XGBoost's regularization differ from a vanilla gradient boosted tree, and why does it help?
- When would you choose a gradient boosted tree over a neural network for a tabular task?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
GBDT vs neural networks for ranking — when do you pick each?
When would you use monotonic constraints in gradient boosted trees?
How do decision trees split? Walk me through Gini impurity vs information gain.
Explain SVMs and kernels from an interviewer's point of view