Machine Learning · hard

What is gradient boosting and how does it differ from bagging?

Asked in ML Engineer interviews, in the Machine Learning round.

Short answer

Boosting fits trees sequentially on residuals to reduce bias, bagging trains them independently to reduce variance.

Bagging (random forest)Boosting (GBM, XGBoost)
Trees are builtIn parallel, independentlyIn sequence, each on the residuals
Each tree seesA bootstrap sampleThe errors still left over
Mainly reducesVarianceBias
Base learnerDeep trees, low biasShallow trees, weak learners
Main riskLittle, hard to overfitOverfits if you boost too long
Tuning that mattersNumber of trees, features per splitLearning rate, depth, early stopping
Bagging averages independent trees. Boosting builds each tree on the last one's mistakes.

How to answer it

Definition

Boosting builds trees in sequence, each one fitted to the errors the previous ones are still making. Bagging builds them independently and averages. The table above compares them; the difference in what they fix is the point.

How boosting works

1. Start with a constant prediction, usually the mean
2. Compute the residuals: what the current model gets wrong
3. Fit a small tree to those residuals
4. Add it to the model, scaled by the learning rate
5. Repeat

Each tree is a weak learner, typically only 3 to 6 levels deep. The strength comes from the sequence, not from any individual tree.

Why the learning rate matters

learning rate 0.3, 100 trees      fast, and prone to overfitting
learning rate 0.03, 1000 trees    slower, and usually generalises better

The learning rate shrinks each tree's contribution, so more trees are needed and no single one dominates. Low rate with many trees and early stopping is the standard recipe.

The key difference

Bagging     parallel, independent trees, each deep
            attacks VARIANCE by averaging
            more trees converges and never overfits

Boosting    sequential, each tree fits what is left over, each shallow
            attacks BIAS by refining
            more trees DOES eventually overfit, so early stopping matters

Key points

FAQ

The 30-second answer

Gradient boosting builds trees in sequence, where each new tree is fitted to the errors the model so far still makes, and is added scaled by a learning rate. The trees are deliberately shallow, so the strength comes from the sequence rather than from any one of them.
Bagging, like a random forest, does the opposite: it builds deep trees independently on bootstrap samples and averages them. So bagging attacks variance by averaging, and boosting attacks bias by refining.
The practical consequence is that adding trees to a forest converges harmlessly, while adding rounds to a boosted model eventually overfits, so I would use a low learning rate with many trees and early stopping on a validation set.

In the interview

Practice this for real