CLASSICAL ML Β· INTERVIEW QUESTION
Explain the difference between bagging and boosting. When would you use each?
π‘ intermediate Β· Ensemble Methods
Hint: Bagging reduces variance, boosting reduces bias. Think about how they combine weak learners.
Answer
Bagging and Boosting are ensemble strategies that combine weak learners, but they target different problems.
Comparison
| Bagging | Boosting | |
|---|---|---|
| Training | Parallel | Sequential |
| Targets | High variance | High bias |
| Sampling | Bootstrap (random subsets) | Weighted (focus on errors) |
| Example | Random Forest | XGBoost, LightGBM |
| Overfitting risk | Low | Higher |
Bagging (Bootstrap Aggregating)
Trains multiple models on random subsets in parallel, combines via averaging/voting. Each model is independent.
Boosting
Trains models sequentially β each new model focuses on errors of previous models. Final prediction is a weighted sum.
python
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
# Bagging: Random Forest
rf = RandomForestClassifier(n_estimators=100, max_depth=10)
# Boosting: Gradient Boosting
gb = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)When to Use
- Bagging: high variance models (deep trees), noisy data
- Boosting: maximum predictive performance, tabular data competitions
XGBoost/LightGBM are the go-to for tabular data. Random Forest when robustness to noise matters.
Related questions
- What is overfitting? How do you detect and prevent it?
- Compare K-Means, DBSCAN, and hierarchical clustering. When would you use each?
- How do you handle missing data in ML? Compare different imputation strategies.
- What is cluster analysis and what are its main applications?
- How do you determine the optimal number of clusters?