Ensemble Learning ✦

ML / Placement Interview Notes β€” the "combine weak learners" chapter

Big idea: a single model has error = biasΒ² + variance + noise. Ensembles combine multiple models so that their errors don't add up the same way β€” bagging kills variance, boosting kills bias. That one line answers 50% of ensemble interview questions.


1. Bagging (Bootstrap Aggregating)

Concept

Why variance drops (the math intuition): if you average N models each with variance σ² and pairwise correlation ρ, the variance of the average is:
Var = ρσ² + (1-ρ)σ²/N
As Nβ†’βˆž, the second term vanishes, but the ρσ² term stays. That's why bagging alone isn't enough β€” you also need to decorrelate the trees. That's exactly what Random Forest adds.

Random Forest = Bagging + Feature Randomness

Feature Importance in RF (2 common methods)

"Why does RF reduce variance?" β†’ Say: bootstrap sampling + random feature subsets at each split β†’ decorrelates trees β†’ averaging decorrelated high-variance, low-bias trees cancels out noise. Don't just say "because it averages many trees" β€” interviewers want the decorrelation part.

OOB (Out-of-Bag) error: since each tree only trains on ~63% of rows, the remaining ~37% ("out of bag") can be used as a free validation set per tree β€” no need for separate cross-validation. Great follow-up fact to mention.


2. Boosting

Concept β€” sequential error correction

Train weak model 1
β†’
Find its errors
β†’
Train model 2 focused on those errors
β†’
Repeat & combine (weighted sum)

Unlike bagging (parallel, independent), boosting is sequential β€” each new learner is trained to fix what previous learners got wrong. This directly attacks bias (underfitting), at some cost of variance/overfitting if you boost too long.

AdaBoost (Adaptive Boosting)

Formula flavor (good to mention, don't need to derive): Ξ±_t = 0.5 Β· ln((1-err_t)/err_t) β€” lower error β†’ higher Ξ± β†’ more voting power.

Gradient Boosting β€” the general idea

Bagging vs Boosting one-liner: Bagging = parallel, reduces variance, uses full-depth trees. Boosting = sequential, reduces bias, uses shallow/weak trees (stumps).

3. XGBoost vs LightGBM vs CatBoost

All three are gradient boosting implementations β€” the core idea (fit trees to gradients) is same. Interviewers want to know the engineering differences:

AspectXGBoostLightGBMCatBoost
Tree growth Level-wise (splits all nodes at a depth before going deeper) Leaf-wise (splits the leaf with max loss reduction) β†’ deeper, more accurate trees, faster convergence, but can overfit on small data Symmetric / oblivious trees (same split condition across a whole level) β†’ faster inference, natural regularization
Split finding Exact greedy + approximate histogram (in newer versions) Histogram-based from the start β€” buckets continuous features into discrete bins, so it evaluates splits on ~256 bins instead of every unique value β†’ much faster Also histogram-based; adds ordered boosting to reduce target leakage/overfitting
Categorical features Needs manual encoding (one-hot / label / target encoding) Can take categorical columns directly (integer-encoded + special handling), still often benefits from encoding Best-in-class native handling β€” uses ordered target statistics (a leak-safe form of target encoding) so you can feed raw categorical columns directly
Regularization L1 (alpha) & L2 (lambda) on leaf weights + gamma (min loss reduction to split) β€” very tunable, "the regularized GBM" Similar params (lambda_l1/l2) + leaf-wise growth needs num_leaves/max_depth capped to avoid overfitting Ordered boosting itself acts as regularization against prediction shift/leakage
Speed on large data Good, but slower than LightGBM on very large/high-dim data Fastest β€” histogram binning + GOSS (Gradient-based One-Side Sampling: keeps high-gradient samples, samples low-gradient ones) + EFB (Exclusive Feature Bundling: merges sparse mutually-exclusive features) Slower to train than LightGBM, but often needs less tuning and handles categoricals/small data well
Best for General purpose, tabular Kaggle default, strong docs/support Huge datasets, many features, speed-critical pipelines Data with lots of categorical columns, want good defaults with less tuning
One-liner to say out loud: "XGBoost = regularized GBM done right, LightGBM = XGBoost optimized for speed via histogram+leaf-wise+GOSS/EFB, CatBoost = optimized for categorical features + prevents leakage via ordered boosting."

Code β€” quick fit (both look almost identical)

# XGBoost
import xgboost as xgb

model = xgb.XGBClassifier(
    n_estimators=300,     # number of boosting rounds (trees). Too high = overfit, use with early_stopping
    learning_rate=0.05,   # shrinkage 'eta' β€” how much each tree corrects; lower = needs more trees but generalizes better
    max_depth=4,          # depth per tree β€” controls model complexity (bias-variance knob)
    subsample=0.8,        # row sampling per tree β€” adds randomness, reduces overfitting
    colsample_bytree=0.8, # feature sampling per tree β€” like RF's random subspace trick
    reg_lambda=1.0,       # L2 regularization on leaf weights
    eval_metric="logloss"
)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)


# LightGBM
import lightgbm as lgb

model = lgb.LGBMClassifier(
    n_estimators=300,
    learning_rate=0.05,
    max_depth=-1,          # -1 = no limit; LightGBM controls complexity via num_leaves instead
    num_leaves=31,        # key LightGBM param (leaf-wise growth) β€” roughly 2^max_depth, tune together with max_depth
    subsample=0.8,
    colsample_bytree=0.8
)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)])

4. Stacking

Quick contrast: Bagging β†’ parallel, same algorithm, averaging. Boosting β†’ sequential, same algorithm, weighted additive. Stacking β†’ parallel, different algorithms, combination is learned by a meta-model.

Random Forest vs XGBoost β€” when to pick which

CriterionRandom ForestXGBoost
MechanismParallel bagging of deep treesSequential boosting of shallow trees
HandlesHigh variance / overfitting-prone base learners β†’ averages it outHigh bias base learners β†’ sequentially corrects errors
Tuning effortLow β€” fairly robust with default params (n_estimators, max_features)High β€” many interacting hyperparams (lr, depth, subsample, regularization) need careful tuning/early stopping
Training speedFast, easily parallelizable (trees are independent)Slower per tree (sequential dependency), though histogram tricks help
Overfitting riskLower, hard to overfit badly even with many treesHigher if learning rate/depth/n_estimators not controlled
Accuracy ceiling (tabular data)Good, often slightly below boosting on structured/tabular competitionsUsually higher, near state-of-the-art on tabular data when tuned well
Robustness to noisy data/outliersMore robust (averaging smooths noise)More sensitive (sequential fitting can chase noisy residuals) β€” needs regularization
InterpretabilitySimple feature importance, easy to explain to non-ML stakeholdersSHAP values commonly used, slightly more complex story
Pick it when...Need a fast, low-maintenance, hard-to-mess-up baseline; limited time to tune; noisy/small data; parallel compute availableNeed best possible accuracy, have time/compute to tune, data is clean-ish, tabular competition or production model where marginal gains matter
Classic follow-up: "Which would you use as a quick baseline vs a final production model?" β†’ RF as baseline (fast, robust, minimal tuning) β†’ XGBoost/LightGBM for the tuned final model once you have time to optimize hyperparameters.

β€” end of notes, good luck! ✦ β€”