Skip to main content

Ensemble Methods

Ensemble Methods - Multiple Models Voting

The Wisdom of Crowds

Question: Who’s smarter - one expert or 100 average people? Surprisingly: The crowd often wins!

A Real Experiment

In 1906, statistician Francis Galton visited a county fair. 787 people guessed the weight of an ox:
  • Individual guesses ranged wildly
  • Average of all guesses: 1,197 pounds
  • Actual weight: 1,198 pounds
The crowd was off by 1 pound! This is the core idea behind ensemble learning:
Many weak learners combined can outperform a single strong learner
Credit Scoring with Ensemble

Why Ensembles Work

Imagine 5 decision trees, each 70% accurate:
The Math: For majority voting with independent 70% accurate models:P(majority correct)=k=35(5k)(0.7)k(0.3)5k83.7%P(\text{majority correct}) = \sum_{k=3}^{5} \binom{5}{k} (0.7)^k (0.3)^{5-k} \approx 83.7\%

Bagging: Bootstrap Aggregating

Idea: Train multiple models on different random samples of data.

How Bagging Works

  1. Create N random samples (with replacement) from training data
  2. Train a model on each sample
  3. Average predictions (regression) or vote (classification)

Random Forest: Bagging + Feature Randomness

Random Forest = Bagging + Random Feature Selection At each split, only consider a random subset of features (typically sqrt(n_features) for classification, n_features/3 for regression). This makes trees more diverse, improving ensemble performance. Here’s the key insight: if one feature is extremely predictive (like “credit score” for loan approval), regular bagging would make every tree split on that feature first, and all trees would look nearly identical. By forcing each tree to sometimes “ignore” the best feature, we create a diverse committee where each tree brings a different perspective. Diversity is the secret sauce of ensembles — correlated models don’t add value, but diverse models cancel each other’s errors.

Feature Importance

Random Forests tell you which features matter most:

Boosting: Learning from Mistakes

Key Idea: Train models sequentially, each focusing on what previous models got wrong.

AdaBoost (Adaptive Boosting)

  1. Train a model
  2. Increase weights of misclassified samples
  3. Train next model (focuses on hard examples)
  4. Combine all models with weighted voting

Gradient Boosting

Instead of reweighting samples, fit each tree to the residual errors — the gap between what we predicted and what actually happened: New Model=Previous Model+Learning Rate×Tree that predicts errors\text{New Model} = \text{Previous Model} + \text{Learning Rate} \times \text{Tree that predicts errors} Think of it like a team of editors reviewing a document. The first editor makes a rough draft. The second editor only focuses on fixing the first editor’s mistakes. The third editor fixes what the second one missed. Each editor makes the document incrementally better, and the learning rate controls how much you trust each editor’s corrections (a small learning rate means “make cautious edits,” which usually works better).

XGBoost: The Competition Winner

XGBoost (Extreme Gradient Boosting) is often the best choice for tabular data.

Why XGBoost Wins

  • Regularization: Built-in L1/L2 regularization prevents overfitting without you having to think about it
  • Parallel training: Uses all CPU cores — tree building is parallelized at the split-finding level
  • Missing values: Handles them automatically by learning the optimal default direction at each split
  • Optimized: Carefully engineered for speed with histogram-based splitting and cache-aware computation
When to reach for XGBoost vs Random Forest: XGBoost typically achieves 1-3% higher accuracy on tabular data but requires more careful tuning (learning_rate, max_depth, n_estimators interact heavily). Random Forest is more “set and forget.” If you have time to tune, use XGBoost. If you need a quick, reliable baseline, use Random Forest.

Comparison: When to Use What?


Bagging vs Boosting

Bagging (Random Forest)

  • Train in parallel
  • Reduce variance (overfitting)
  • Works with high-variance models
  • More robust to outliers
  • Harder to overfit

Boosting (XGBoost)

  • Train sequentially
  • Reduce bias (underfitting)
  • Learns from mistakes
  • Usually more accurate
  • Can overfit if not tuned

Hyperparameter Tuning


Voting Classifier: Mix Different Models

Combine different types of models:

Stacking: Models Learn from Models

Train a meta-model on the predictions of base models. The idea: each base model has different strengths and weaknesses. A logistic regression meta-learner can figure out “trust the Random Forest on samples like these, but trust the SVM on samples like those.”

🚀 Mini Projects

Project 1

Build and tune a Random Forest classifier

Project 2

Gradient Boosting for regression

Project 3

Ensemble comparison on real dataset

Key Takeaways

Crowd Wisdom

Many weak models beat one strong model

Bagging = Parallel

Train on different data samples

Boosting = Sequential

Each model fixes previous mistakes

Random Forest

Best starting point for tabular data

What’s Next?

Now that you understand the main ML algorithms, let’s learn how to properly evaluate and compare models!

Continue to Module 7: Model Evaluation

Learn cross-validation, metrics, and how to avoid common mistakes