Boosting and Bagging

Ensemble Methods for Stronger Machine Learning

Ensemble Fundamentals

Understand why combining models improves performance

Bagging Methods

Master bootstrap aggregating and random forests

Boosting Algorithms

Learn AdaBoost, gradient boosting, and XGBoost

Practical Applications

Apply ensemble methods to real-world problems

Why Ensemble Methods Work

Ensemble methods combine multiple models to achieve better performance than any individual model, leveraging the wisdom of crowds principle to reduce errors and improve robustness.

Bias-Variance Decomposition

$$\text{Error} = \text{Bias}^2 + \text{Variance} + \text{Noise}$$
  • Bagging: Reduces variance by averaging
  • Boosting: Reduces bias by sequential learning
  • Stacking: Learns optimal combination weights
  • Diversity: Key to ensemble success
Intuition: If models make different types of errors, averaging their predictions can cancel out individual mistakes.
Single Model vs Ensemble Performance

Bootstrap Aggregating (Bagging)

Algorithm Steps

1. Bootstrap Sampling

Create B bootstrap samples by sampling n examples with replacement from training set

2. Train Base Models

Train a model on each bootstrap sample independently

3. Aggregate Predictions

Average predictions (regression) or vote (classification)

Mathematical Foundation

$$\hat{f}_{\text{bag}}(x) = \frac{1}{B}\sum_{b=1}^{B} \hat{f}_b(x)$$

Where $\hat{f}_b$ is the model trained on bootstrap sample b.

Variance Reduction

$$\text{Var}(\bar{X}) = \frac{\sigma^2}{n}$$

Averaging reduces variance by factor of n (if uncorrelated).

Bagging Process Visualization

Random Forests: Bagging + Feature Randomness

Random forests extend bagging by adding feature randomness, selecting a random subset of features at each split to decorrelate trees and improve generalization.

Key Innovations

  • Bootstrap Sampling: Each tree trained on different subset
  • Feature Randomness: Random features at each split
  • No Pruning: Trees grown deep to reduce bias
  • Out-of-Bag Estimation: Internal validation

Hyperparameters

  • n_estimators: Number of trees (100-1000)
  • max_features: Features per split (√p for classification)
  • max_depth: Tree depth (None for full growth)
  • min_samples_split: Minimum samples to split
Random Forest Architecture
Out-of-Bag Error: Each example is "out-of-bag" for ~37% of trees, enabling unbiased error estimation without separate validation set.

AdaBoost: Adaptive Boosting

Algorithm Steps

1. Initialize Weights

$$w_i^{(1)} = \frac{1}{n}$$

2. Train Weak Learner

Find classifier that minimizes weighted error

$$\epsilon_t = \sum_{i: h_t(x_i) \neq y_i} w_i^{(t)}$$

3. Compute Classifier Weight

$$\alpha_t = \frac{1}{2}\ln\left(\frac{1-\epsilon_t}{\epsilon_t}\right)$$

4. Update Example Weights

$$w_i^{(t+1)} = w_i^{(t)} \exp(-\alpha_t y_i h_t(x_i))$$

Increase weights for misclassified examples

5. Final Prediction

$$H(x) = \text{sign}\left(\sum_{t=1}^{T} \alpha_t h_t(x)\right)$$
Key Insight: AdaBoost focuses on hard examples by increasing their weights, forcing subsequent classifiers to pay more attention to previously misclassified cases.

Gradient Boosting Machines

Gradient boosting fits models sequentially, with each new model trained to predict the residuals (errors) of the ensemble so far, using gradient descent in function space.

Algorithm Framework

$$F_m(x) = F_{m-1}(x) + \gamma_m h_m(x)$$

Where $h_m$ is trained on negative gradients of loss function.

Gradient Computation

$$r_{im} = -\left[\frac{\partial L(y_i, F(x_i))}{\partial F(x_i)}\right]_{F=F_{m-1}}$$

Pseudo-residuals guide next model training.

Loss Functions

  • Squared Loss: $(y - F(x))^2$ for regression
  • Absolute Loss: $|y - F(x)|$ for robust regression
  • Logistic Loss: Cross-entropy for classification
  • Quantile Loss: For quantile regression
Regularization:
  • Learning rate (shrinkage): 0.01-0.3
  • Tree depth: 3-8 levels
  • Subsampling: 0.5-0.8 fraction
  • Early stopping on validation

XGBoost: Extreme Gradient Boosting

Key Innovations

  • Regularized Objective: L1 and L2 penalties on leaf weights
  • Second-Order Gradients: Newton's method approximation
  • Parallel Processing: Parallelized tree construction
  • Missing Value Handling: Learns optimal direction
  • Built-in Cross-Validation: Automatic early stopping

Objective Function

$$\mathcal{L}^{(t)} = \sum_{i=1}^{n} l(y_i, \hat{y}_i^{(t-1)} + f_t(x_i)) + \Omega(f_t)$$

Regularization Term

$$\Omega(f) = \gamma T + \frac{1}{2}\lambda \sum_{j=1}^{T} w_j^2$$

Where T is number of leaves, $w_j$ are leaf weights.

Framework
Strengths
Use Cases
XGBoost
High performance, feature importance
Tabular data, competitions
LightGBM
Memory efficient, fast training
Large datasets, real-time
CatBoost
Categorical features, no preprocessing
Mixed data types, minimal tuning

Bagging vs Boosting: Key Differences

Aspect
Bagging
Boosting
Training
Parallel (independent)
Sequential (dependent)
Focus
Reduce variance
Reduce bias
Base Models
Strong learners (deep trees)
Weak learners (stumps)
Overfitting
Less prone to overfit
Can overfit with noise
Computational
Easily parallelizable
Inherently sequential
Robustness
Robust to outliers
Sensitive to outliers
Bagging vs Boosting Learning Curves

Advanced Ensemble Techniques

Stacking (Stacked Generalization)

  • Level 0: Base models trained on data
  • Level 1: Meta-learner combines base predictions
  • Cross-validation: Prevents overfitting to base models
  • Diversity: Use different algorithm types

Blending

  • Holdout set for meta-learner training
  • Simpler than full stacking
  • Less prone to overfitting
  • Popular in competitions

Ensemble Diversity

  • Algorithm Diversity: Different model types
  • Data Diversity: Different features/samples
  • Parameter Diversity: Different hyperparameters
  • Training Diversity: Different training procedures
Best Practices:
  • Combine uncorrelated models
  • Balance individual accuracy with diversity
  • Use cross-validation for model selection
  • Monitor ensemble complexity

Ensemble Success Formula: High individual accuracy + Low correlation between models = Strong ensemble performance

Implementation Guidelines and Best Practices

When to Use Each Method

  • Random Forests: General-purpose, interpretable, robust baseline
  • Gradient Boosting: High accuracy needed, careful tuning possible
  • XGBoost/LightGBM: Tabular data competitions, feature importance
  • Bagging: High-variance models, parallel processing available

Common Pitfalls

  • Overfitting with too many boosting rounds
  • Using correlated base models
  • Ignoring computational constraints
  • Not validating ensemble diversity

Hyperparameter Tuning

  • Random Forest: n_estimators, max_features, max_depth
  • XGBoost: learning_rate, max_depth, subsample, reg_alpha/lambda
  • Early Stopping: Use validation set to prevent overfitting
  • Cross-Validation: K-fold for robust performance estimates
Performance Tips:
  • Start with Random Forest baseline
  • Use learning curves for boosting
  • Monitor training vs validation error
  • Consider ensemble of ensembles

Key Takeaway: Ensemble methods consistently achieve state-of-the-art performance on tabular data. They're often the first choice for machine learning competitions and real-world applications where predictive accuracy is paramount.

1 / 10