Ensemble Methods
Better Together — Bagging, Boosting, and Stacking
Ensemble methods combine multiple models to produce predictions more accurate than any single model alone. Diversity between models is the key to their power.
- Bagging — trains models independently on different data samples and averages their predictions to reduce variance
- Boosting — trains models sequentially, with each correcting the errors of the previous ensemble
- Stacking — combines different model types with a meta-learner that learns the optimal way to blend predictions
"If you want to go fast, go alone. If you want to go far, go together."
Ensemble Methods — Complete Guide
Ensemble methods combine multiple models to produce better predictions than any single model.
Types of Ensembles
DfBagging (Bootstrap Aggregating)
A technique where multiple models are trained independently on different random subsets of data (with replacement), then combined to produce a final prediction.
DfBoosting
A technique where models are trained sequentially, with each new model correcting errors made by previous ones. Reduces both bias and variance.
DfStacking
A technique where different model types are trained, and a meta-learner is trained to combine their predictions. Uses diverse base models.
Ensemble Architecture Comparison
Bagging (Bootstrap Aggregating):
Train models INDEPENDENTLY on different data samples
Combine by averaging/voting
Reduces variance
Example: Random Forest
Boosting:
Train models SEQUENTIALLY
Each model corrects previous errors
Reduces bias and variance
Example: XGBoost, AdaBoost
Stacking:
Train different model types
Train a meta-learner to combine predictions
Uses diverse base models
Often wins competitions
Voting:
Hard voting: majority wins
Soft voting: average probabilities
Simple but effective
Why It Matters
Ensemble methods leverage the "wisdom of crowds" — combining multiple models often produces better predictions than any single model. The key is diversity between models.
Mathematical Foundation
Ensemble Error Decomposition
For an ensemble of
models with individual error
and pairwise correlation
:
where
is the average correlation between model errors.
Key insight: Diversity (
) is what makes ensembles work. The more uncorrelated the errors, the more the ensemble reduces variance.
Boosting as Gradient Descent
Boosting can be viewed as gradient descent in function space:
where
is the learning rate and
fits the negative gradient:
Python Implementation
Python Implementation
from sklearn.ensemble import (
VotingClassifier, StackingClassifier,
BaggingClassifier, AdaBoostClassifier
)
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
# Voting
voting = VotingClassifier(estimators=[
('lr', LogisticRegression()),
('rf', RandomForestClassifier()),
('svc', SVC(probability=True))
], voting='soft')
# Bagging
bagging = BaggingClassifier(
base_estimator=DecisionTreeClassifier(),
n_estimators=100, random_state=42
)
# Boosting
boosting = AdaBoostClassifier(
base_estimator=DecisionTreeClassifier(max_depth=1),
n_estimators=100, learning_rate=0.1
)
# Stacking
stacking = StackingClassifier(estimators=[
('rf', RandomForestClassifier()),
('svm', SVC(probability=True)),
('xgb', xgb.XGBClassifier())
], final_estimator=LogisticRegression())
Ensemble Error Analysis
When to Use Each Method
Key Takeaways
Summary: Ensemble Methods
- Ensembles almost always outperform single models
- Bagging reduces variance (Random Forest)
- Boosting reduces bias and variance (XGBoost)
- Stacking combines different model types
- Diversity between models is key to ensemble success
- Voting is simple — good baseline ensemble
- XGBoost is the most popular boosting algorithm
- Ensembles trade interpretability for performance
What to Learn Next
-> Random Forest Dive deep into bagging with Random Forest — the most popular ensemble method.
-> XGBoost Master gradient boosting, the sequential ensemble technique that dominates Kaggle competitions.
-> Decision Trees Understand the base learners that ensemble methods combine for stronger predictions.
-> Model Evaluation Evaluate ensemble performance with cross-validation and understand when ensembles help.
-> Interpretability Use SHAP values to explain black-box ensemble model predictions.
-> AutoML Automate model selection and ensemble construction with automated machine learning.