Ensemble Methods in Machine Learning
Ensemble methods combine the predictions of multiple individual models (base learners) to produce a final prediction that is more accurate and robust than any single model. They are among the most powerful and widely-used techniques in practical machine learning — gradient-boosted trees consistently dominate structured/tabular data competitions, and ensembling is a standard strategy in production systems.
The underlying intuition: diverse models make different errors. When their predictions are aggregated, individual errors cancel out — leaving the signal.
Why Ensembles Work
A key result from bias-variance decomposition: the expected error of a model can be decomposed as:
$$\text{Error} = \text{Bias}^2 + \text{Variance} + \text{Irreducible Noise}$$
- High bias (underfitting) — model is too simple to capture the true pattern.
- High variance (overfitting) — model is too sensitive to training data fluctuations.
Different ensemble strategies target different parts of this trade-off:
| Method | Reduces | Achieved by |
|---|---|---|
| Bagging | Variance | Averaging diverse models |
| Boosting | Bias | Sequential error correction |
| Stacking | Both | Learning a meta-model |
Bagging (Bootstrap Aggregating)
Bagging trains multiple models on different random subsets of the training data (bootstrap samples — sampling with replacement) and averages their predictions.
Algorithm:
- For $m = 1$ to $M$:
- Draw a bootstrap sample $D_m$ of size $n$ from the training set.
- Train model $h_m$ on $D_m$.
- Aggregation:
- Regression: $\hat{y} = \frac{1}{M} \sum_{m=1}^M h_m(x)$
- Classification: majority vote.
The diversity comes from each model seeing a different subset of the data (~63% unique samples per bootstrap, ~37% out-of-bag samples useful for validation).
Random Forests
Random Forests (Breiman, 2001) extend bagging with an additional randomness injection: at each split in each tree, only a random subset of $\sqrt{p}$ features (for classification) is considered. This further decorrelates trees, making the ensemble more robust.
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(
n_estimators=500,
max_features='sqrt',
max_depth=None,
min_samples_leaf=1,
n_jobs=-1,
random_state=42
)
rf.fit(X_train, y_train)
Key strengths:
- Handles high-dimensional data well.
- Built-in feature importance via mean impurity decrease.
- Robust to overfitting due to averaging.
- Out-of-bag (OOB) error estimate without a separate validation set.
Extra-Randomized Trees (ExtraTrees) go further — splitting thresholds are chosen randomly rather than optimally, trading some accuracy for faster training and lower variance.
Boosting
Boosting builds an ensemble sequentially — each new model focuses on the errors of the previous ones. This reduces bias by iteratively correcting mistakes.
AdaBoost
The original boosting algorithm (Freund & Schapire, 1995):
- Initialize uniform sample weights $w_i = 1/n$.
- For $m = 1$ to $M$:
- Train a weak learner $h_m$ minimizing weighted error.
- Compute model weight: $\alpha_m = \frac{1}{2} \ln\frac{1 - \epsilon_m}{\epsilon_m}$ (higher for accurate models).
- Increase weights of misclassified samples: $w_i \leftarrow w_i \cdot e^{\alpha_m \cdot \mathbf{1}[y_i \neq h_m(x_i)]}$.
- Final prediction: $\hat{y} = \text{sign}\left(\sum_m \alpha_m h_m(x)\right)$.
Gradient Boosting
Gradient Boosting (Friedman, 2001) generalizes boosting as gradient descent in function space. Each new tree fits the residuals (pseudo-residuals from the gradient of the loss function):
$$F_m(x) = F_{m-1}(x) + \eta \cdot h_m(x)$$
where $h_m$ is a regression tree fitted to $-\nabla_{F_{m-1}} L(y, F_{m-1}(x))$.
Works with any differentiable loss function — squared error, log-loss, absolute error, custom losses.
XGBoost
XGBoost (Chen & Guestrin, 2016) made gradient boosting fast and scalable via:
- Second-order Taylor approximation of the loss for better split scoring.
- Regularization terms ($L_1$/$L_2$) on leaf weights.
- Approximate split finding for large datasets.
- Column subsampling per tree and per level.
- Sparse-aware algorithm for missing values.
import xgboost as xgb
model = xgb.XGBClassifier(
n_estimators=1000,
learning_rate=0.05,
max_depth=6,
subsample=0.8,
colsample_bytree=0.8,
reg_lambda=1.0,
use_label_encoder=False,
eval_metric='logloss',
early_stopping_rounds=50
)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)
LightGBM
LightGBM (Microsoft, 2017) achieves faster training than XGBoost via:
- Leaf-wise (best-first) tree growth instead of level-wise — grows the leaf with maximum loss reduction.
- Gradient-based One-Side Sampling (GOSS) — retains all high-gradient samples; randomly samples low-gradient ones.
- Exclusive Feature Bundling (EFB) — bundles mutually exclusive sparse features to reduce dimensionality.
Up to 20× faster on large datasets; typically the preferred choice for tabular ML competitions and production systems.
CatBoost
CatBoost (Yandex) is optimized for datasets with many categorical features through ordered target statistics — computing target encodings using only preceding rows to prevent leakage and reduce overfitting.
Stacking (Stacked Generalization)
Stacking trains a meta-learner on the predictions of base models:
- Level 0: Train $K$ diverse base models (e.g., LightGBM, Random Forest, Logistic Regression, SVM) using $k$-fold cross-validation to generate out-of-fold predictions.
- Level 1: Train a meta-learner (often a simple model like Logistic Regression or Ridge) on the base model predictions as features.
Training Data → [RF, XGBoost, LR, SVM] → OOF Predictions → Meta-Learner → Final Prediction
Cross-validation is critical — training base models on the full data and evaluating the meta-learner on the same data would severely overfit.
Stacking consistently outperforms any individual model and is a standard technique in Kaggle competition winning approaches.
Voting and Averaging
The simplest ensembling — combine multiple trained models by:
- Hard voting — each model votes; majority wins.
- Soft voting — average the predicted class probabilities.
- Weighted averaging — weight each model’s output by its validation performance.
Effective even when models are trained on the same data, especially when they use different algorithms or hyperparameter configurations.
Practical Guidance
| Scenario | Recommendation |
|---|---|
| Tabular data, need best accuracy | LightGBM or XGBoost with tuning |
| Tabular data, many categoricals | CatBoost |
| Need fast training + interpretability | Random Forest |
| Competition / maximum accuracy | Stacking + diverse base learners |
| Simple baseline | Soft voting of 3–5 diverse models |
Key principles:
- Diversity beats accuracy — More diverse (different architectures, features, seeds) ensembles outperform ensembles of highly accurate but similar models.
- Avoid data leakage in stacking — always use cross-validation for OOF predictions.
- Early stopping is essential for boosting — monitor a validation set and stop before overfitting.
- Learning rate + trees are the most impactful gradient boosting hyperparameters; lower learning rate + more trees generally beats higher learning rate + fewer trees.