MLCrack

    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

    BaggingBoosting
    TrainingParallelSequential
    TargetsHigh varianceHigh bias
    SamplingBootstrap (random subsets)Weighted (focus on errors)
    ExampleRandom ForestXGBoost, LightGBM
    Overfitting riskLowHigher

    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.