Machine Learning · hard
Asked in ML Engineer interviews, in the Machine Learning round.
Boosting fits trees sequentially on residuals (reduce bias); bagging trains independently (reduce variance).
Bagging trains many models independently on bootstrap samples and averages them; it reduces variance. Boosting trains models one after another, each one fitting what the previous ones got wrong; it reduces bias.
Gradient boosting makes "what they got wrong" precise: at each step, fit a small tree to the negative gradient of the loss with respect to the current predictions. For squared error that gradient is the residual, so the next tree literally predicts the errors of the ensemble so far. Add it in, scaled by a learning rate, and repeat. The model is a sum of small trees, each correcting the last.
from sklearn.ensemble import GradientBoostingRegressor
gb = GradientBoostingRegressor(n_estimators=500, learning_rate=0.05, max_depth=3, subsample=0.8)
# XGBoost / LightGBM / CatBoost: same idea, faster, with regularisation and better handling of missing values
The consequences of sequential training are the interview:
Practical guidance: boosting usually wins on tabular data when tuned; a forest is the robust default when you cannot tune.