Skip to content
VDAI with VD

Phase 02 ยท ML Fundamentals

Ensemble Methods

A group of weak learners, combined correctly, becomes a strong learner. This is not a metaphor. It is a theorem.

  • Build
  • Python
  • 120 minutes
  • Before this: Phase 2, Lesson 10 (Bias-Variance Tradeoff)

Learning Objectives

  • Implement AdaBoost and gradient boosting from scratch and explain how boosting sequentially reduces bias
  • Build a bagging ensemble and demonstrate how averaging decorrelated models reduces variance without increasing bias
  • Compare bagging, boosting, and stacking in terms of what error component each method targets
  • Evaluate ensemble diversity and explain why majority voting accuracy improves with more independent weak learners

The Problem

A single decision tree is fast to train and easy to interpret, but it overfits. A single linear model underfits on complex boundaries. You could spend days engineering the perfect model architecture. Or you could combine a bunch of imperfect models and get something better than any of them individually.

Ensemble methods do exactly this. They are the most reliable technique for winning Kaggle competitions on tabular data, they power most production ML systems, and they illustrate the bias-variance tradeoff in action. Bagging reduces variance. Boosting reduces bias. Stacking learns which models to trust on which inputs.

The Concept

Why Ensembles Work

Suppose you have N independent classifiers, each with accuracy p > 0.5. The majority vote has accuracy:

P(majority correct) = sum over k > N/2 of C(N,k) * p^k * (1-p)^(N-k)

For 21 classifiers each with 60% accuracy, majority vote accuracy is about 74%. With 101 classifiers, it rises to 84%. The errors cancel out when the models make different mistakes.

The key requirement is diversity. If all models make the same errors, combining them helps nothing. Ensembles work because they produce diverse models through:

  • Different training subsets (bagging)
  • Different feature subsets (random forests)
  • Sequential error correction (boosting)
  • Different model families (stacking)

Bagging (Bootstrap Aggregating)

Bagging creates diversity by training each model on a different bootstrap sample of the training data.

flowchart TD
    D[Training Data] --> B1[Bootstrap Sample 1]
    D --> B2[Bootstrap Sample 2]
    D --> B3[Bootstrap Sample 3]
    D --> BN[Bootstrap Sample N]

    B1 --> M1[Model 1]
    B2 --> M2[Model 2]
    B3 --> M3[Model 3]
    BN --> MN[Model N]

    M1 --> V[Average or Majority Vote]
    M2 --> V
    M3 --> V
    MN --> V

    V --> P[Final Prediction]

A bootstrap sample is drawn with replacement from the original data, same size as the original. About 63.2% of unique samples appear in each bootstrap. The remaining 36.8% (out-of-bag samples) provide a free validation set.

Bagging reduces variance without increasing bias much. Each individual tree overfits to its bootstrap sample, but the overfitting is different for each tree, so averaging cancels out the noise.

Random Forests are bagging with an extra twist: at each split, only a random subset of features is considered. This forces even more diversity among trees. The typical number of candidate features is sqrt(n_features) for classification and n_features / 3 for regression.

Boosting (Sequential Error Correction)

Boosting trains models sequentially. Each new model focuses on the examples that previous models got wrong.

flowchart LR
    D[Data with weights] --> M1[Model 1]
    M1 --> E1[Find errors]
    E1 --> W1[Increase weights on errors]
    W1 --> M2[Model 2]
    M2 --> E2[Find errors]
    E2 --> W2[Increase weights on errors]
    W2 --> M3[Model 3]
    M3 --> F[Weighted sum of all models]

Boosting reduces bias. Each new model corrects the systematic errors of the ensemble so far. The final prediction is a weighted sum of all models, where better models get higher weights.

The tradeoff: boosting can overfit if you run too many rounds, because it keeps fitting harder examples, some of which may be noise.

AdaBoost

AdaBoost (Adaptive Boosting) was the first practical boosting algorithm. It works with any base learner, typically decision stumps (depth-1 trees).

The algorithm:

1. Initialize sample weights: w_i = 1/N for all i

2. For t = 1 to T:
   a. Train weak learner h_t on weighted data
   b. Compute weighted error:
      err_t = sum(w_i * I(h_t(x_i) != y_i)) / sum(w_i)
   c. Compute model weight:
      alpha_t = 0.5 * ln((1 - err_t) / err_t)
   d. Update sample weights:
      w_i = w_i * exp(-alpha_t * y_i * h_t(x_i))
   e. Normalize weights to sum to 1

3. Final prediction: H(x) = sign(sum(alpha_t * h_t(x)))

Models with lower error get higher alpha. Misclassified samples get higher weights so the next model focuses on them.

Gradient Boosting

Gradient boosting generalizes boosting to arbitrary loss functions. Instead of reweighting samples, it fits each new model to the residuals (negative gradient of the loss) of the current ensemble.

1. Initialize: F_0(x) = argmin_c sum(L(y_i, c))

2. For t = 1 to T:
   a. Compute pseudo-residuals:
      r_i = -dL(y_i, F_{t-1}(x_i)) / dF_{t-1}(x_i)
   b. Fit a tree h_t to the residuals r_i
   c. Find optimal step size:
      gamma_t = argmin_gamma sum(L(y_i, F_{t-1}(x_i) + gamma * h_t(x_i)))
   d. Update:
      F_t(x) = F_{t-1}(x) + learning_rate * gamma_t * h_t(x)

3. Final prediction: F_T(x)

For squared error loss, the pseudo-residuals are just the actual residuals: r_i = y_i - F_{t-1}(x_i). Each tree literally fits the errors of the previous ensemble.

The learning rate (shrinkage) controls how much each tree contributes. Smaller learning rates require more trees but generalize better. Typical values: 0.01 to 0.3.

XGBoost: Why It Dominates Tabular Data

XGBoost (eXtreme Gradient Boosting) is gradient boosting with engineering optimizations that make it fast, accurate, and resistant to overfitting:

  • Regularized objective: L1 and L2 penalties on leaf weights prevent individual trees from being too confident
  • Second-order approximation: Uses both first and second derivatives of the loss, giving better split decisions
  • Sparsity-aware splits: Handles missing values natively by learning the best direction for missing data at each split
  • Column subsampling: Like random forests, samples features at each split for diversity
  • Weighted quantile sketch: Efficiently finds split points for continuous features on distributed data
  • Cache-aware block structure: Memory layout optimized for CPU cache lines

For tabular data, XGBoost (and its successor LightGBM) consistently outperforms neural networks. This is not changing anytime soon. If your data fits in a table with rows and columns, start with gradient boosting.

Stacking (Meta-Learning)

Stacking uses the predictions of multiple base models as features for a meta-learner.

flowchart TD
    D[Training Data] --> M1[Model 1: Random Forest]
    D --> M2[Model 2: SVM]
    D --> M3[Model 3: Logistic Regression]

    M1 --> P1[Predictions 1]
    M2 --> P2[Predictions 2]
    M3 --> P3[Predictions 3]

    P1 --> META[Meta-Learner]
    P2 --> META
    P3 --> META

    META --> F[Final Prediction]

The meta-learner learns which base model to trust for which inputs. If the random forest is better at certain regions and the SVM at others, the meta-learner will learn to route accordingly.

To avoid data leakage, base model predictions must be generated via cross-validation on the training set. You never train base models and generate meta-features on the same data.

Voting

The simplest ensemble. Just combine predictions directly.

  • Hard voting: Majority vote on class labels.
  • Soft voting: Average predicted probabilities, pick the class with highest average probability. Usually better because it uses confidence information.

Interactive figure coming later.

Build It

Step 1: Decision Stump (Base Learner)

The code in code/ensembles.py implements everything from scratch. We start with a decision stump: a tree with a single split.

class DecisionStump:
    def __init__(self):
        self.feature_idx = None
        self.threshold = None
        self.polarity = 1
        self.alpha = None

    def fit(self, X, y, weights):
        n_samples, n_features = X.shape
        best_error = float("inf")

        for f in range(n_features):
            thresholds = np.unique(X[:, f])
            for thresh in thresholds:
                for polarity in [1, -1]:
                    pred = np.ones(n_samples)
                    pred[polarity * X[:, f] < polarity * thresh] = -1
                    error = np.sum(weights[pred != y])
                    if error < best_error:
                        best_error = error
                        self.feature_idx = f
                        self.threshold = thresh
                        self.polarity = polarity

    def predict(self, X):
        n = X.shape[0]
        pred = np.ones(n)
        idx = self.polarity * X[:, self.feature_idx] < self.polarity * self.threshold
        pred[idx] = -1
        return pred

Step 2: AdaBoost from Scratch

class AdaBoostScratch:
    def __init__(self, n_estimators=50):
        self.n_estimators = n_estimators
        self.stumps = []
        self.alphas = []

    def fit(self, X, y):
        n = X.shape[0]
        weights = np.full(n, 1 / n)

        for _ in range(self.n_estimators):
            stump = DecisionStump()
            stump.fit(X, y, weights)
            pred = stump.predict(X)

            err = np.sum(weights[pred != y])
            err = np.clip(err, 1e-10, 1 - 1e-10)

            alpha = 0.5 * np.log((1 - err) / err)
            weights *= np.exp(-alpha * y * pred)
            weights /= weights.sum()

            stump.alpha = alpha
            self.stumps.append(stump)
            self.alphas.append(alpha)

    def predict(self, X):
        total = sum(a * s.predict(X) for a, s in zip(self.alphas, self.stumps))
        return np.sign(total)

Step 3: Gradient Boosting from Scratch

class GradientBoostingScratch:
    def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
        self.n_estimators = n_estimators
        self.lr = learning_rate
        self.max_depth = max_depth
        self.trees = []
        self.initial_pred = None

    def fit(self, X, y):
        self.initial_pred = np.mean(y)
        current_pred = np.full(len(y), self.initial_pred)

        for _ in range(self.n_estimators):
            residuals = y - current_pred
            tree = SimpleRegressionTree(max_depth=self.max_depth)
            tree.fit(X, residuals)
            update = tree.predict(X)
            current_pred += self.lr * update
            self.trees.append(tree)

    def predict(self, X):
        pred = np.full(X.shape[0], self.initial_pred)
        for tree in self.trees:
            pred += self.lr * tree.predict(X)
        return pred

Step 4: Compare against sklearn

The code verifies that our from-scratch implementations produce similar accuracy to sklearn's AdaBoostClassifier and GradientBoostingClassifier, and compares all methods side by side.

Use It

When to Use Each Method

MethodReducesBest forWatch out for
Bagging / Random ForestVarianceNoisy data, many featuresDoes not help with bias
AdaBoostBiasClean data, simple base learnersSensitive to outliers and noise
Gradient BoostingBiasTabular data, competitionsSlow to train, easy to overfit without tuning
XGBoost / LightGBMBothProduction tabular MLMany hyperparameters
StackingBothGetting last 1-2% accuracyComplex, risk of overfitting meta-learner
VotingVarianceQuick combination of diverse modelsOnly helps if models are diverse

The Production Stack for Tabular Data

For most tabular prediction problems, this is the order to try:

  1. LightGBM or XGBoost with default parameters
  2. Tune n_estimators, learning_rate, max_depth, min_child_weight
  3. If you need the last 0.5%, build a stacking ensemble with 3-5 diverse models
  4. Use cross-validation throughout

Neural networks on tabular data are almost always worse than gradient boosting, despite continued research attempts. TabNet, NODE, and similar architectures occasionally match but rarely beat a well-tuned XGBoost.

Ship It

This lesson produces outputs/prompt-ensemble-selector.md -- a prompt that helps you pick the right ensemble method for a given dataset. Describe your data (size, feature types, noise level, class balance) and the problem you are solving. The prompt walks through a decision checklist, recommends a method, suggests starting hyperparameters, and warns about common mistakes for that method. Also produces outputs/skill-ensemble-builder.md with the full selection guide.

Exercises

  1. Modify the AdaBoost implementation to track training accuracy after each round. Plot accuracy vs. number of estimators. When does it converge?

  2. Implement a random forest from scratch by adding random feature subsampling to the regression tree. Train 100 trees with max_features=sqrt(n_features) and average predictions. Compare variance reduction to a single tree.

  3. In the gradient boosting implementation, add early stopping: track validation loss after each round and stop when it has not improved for 10 consecutive rounds. How many trees does it actually need?

  4. Build a stacking ensemble with three base models (logistic regression, decision tree, k-nearest neighbors) and a logistic regression meta-learner. Use 5-fold cross-validation to generate meta-features. Compare to each base model alone.

  5. Run XGBoost on the same dataset with default parameters. Compare its accuracy to your from-scratch gradient boosting. Time both. How large is the speed difference?

Key Terms

TermWhat people sayWhat it actually means
Bagging"Train on random subsets"Bootstrap aggregating: train models on bootstrap samples, average predictions to reduce variance
Boosting"Focus on hard examples"Train models sequentially, each correcting errors of the ensemble so far, to reduce bias
AdaBoost"Reweight the data"Boosting via sample weight updates; misclassified points get higher weight for the next learner
Gradient boosting"Fit the residuals"Boosting via fitting each new model to the negative gradient of the loss function
XGBoost"The Kaggle weapon"Gradient boosting with regularization, second-order optimization, and systems-level speed tricks
Stacking"Models on top of models"Use predictions of base models as input features for a meta-learner
Random forest"Many randomized trees"Bagging with decision trees, adding random feature subsampling at each split for diversity
Ensemble diversity"Make different mistakes"Models must be uncorrelated in their errors for the ensemble to improve over individuals
Out-of-bag error"Free validation"Samples not in a bootstrap draw (~36.8%) serve as a validation set without needing a holdout

Further Reading

Code

Take-away files

prompt-ensemble-selector.md

---
name: prompt-ensemble-selector
description: Pick the right ensemble method for a given dataset and problem
phase: 02
lesson: 11
---

You are an ensemble method selector. Given a description of a dataset and a prediction problem, you recommend the best ensemble approach with specific configuration advice.

When a user describes their data and problem, work through each section below.

## Step 1: Understand the data

Ask about and summarize:
- Number of rows (under 1k, 1k-100k, over 100k)
- Number of features and their types (numeric, categorical, mixed)
- Class balance (for classification) or target distribution (for regression)
- Noise level: is the data clean or noisy with outliers?
- Whether there are missing values

## Step 2: Identify the core issue

Determine the primary modeling challenge:
- High variance (model overfits, large gap between train and test scores): bagging territory
- High bias (model underfits, both train and test scores are low): boosting territory
- Need maximum accuracy with compute to spare: stacking territory
- Quick baseline needed with minimal tuning risk: Random Forest

## Step 3: Recommend a method

Based on the data profile and core issue, recommend one primary method and one alternative:

**Small data (under 1k rows):** Random Forest. Boosting methods overfit easily on small data. Random Forest is nearly impossible to misconfigure.

**Medium data (1k-100k rows), clean:** XGBoost or LightGBM. Start with learning_rate=0.1 and use early stopping on a validation set. These give the best accuracy-to-effort ratio.

**Medium data, noisy with outliers:** Random Forest. Bagging is robust to noise because outliers affect individual trees differently and averaging cancels out their influence.

**Large data (100k+ rows):** LightGBM. Its histogram-based splits and leaf-wise growth make it the fastest gradient boosting implementation. XGBoost works too but is slower at this scale.

**Many categorical features:** CatBoost. It handles categoricals natively without one-hot encoding, which avoids the curse of dimensionality from high-cardinality features.

**Need the last 1-2% accuracy:** Stacking with 3-5 diverse base models (e.g., Random Forest + XGBoost + logistic regression + SVM). Always generate base model predictions via cross-validation.

**Quick combination of existing models:** Soft voting. Average predicted probabilities from 2-3 already-trained models. No meta-learner needed.

## Step 4: Suggest starting hyperparameters

For the recommended method, provide specific starting values:

**Random Forest:**
- n_estimators: 200
- max_depth: None (let trees grow fully)
- max_features: "sqrt" for classification, n_features/3 for regression
- min_samples_leaf: 1-5

**XGBoost / LightGBM:**
- learning_rate: 0.1
- n_estimators: 1000 with early_stopping_rounds=50
- max_depth: 6
- subsample: 0.8
- colsample_bytree: 0.8

**Stacking:**
- Base models: at least 3, from different families
- Meta-learner: logistic regression (classification) or ridge regression (regression)
- Use 5-fold cross-validation for generating meta-features

## Step 5: Warn about pitfalls

Flag the most common mistakes for the recommended method:
- Gradient boosting without early stopping will overfit
- Random Forest will not fix underfitting (it reduces variance, not bias)
- Stacking with similar base models provides no diversity benefit
- AdaBoost on noisy data amplifies outliers each round
- Setting learning_rate above 0.3 in gradient boosting causes instability

## Output format

Structure your response as:
1. **Data profile**: size, types, noise, balance
2. **Core issue**: variance, bias, or both
3. **Recommended method**: primary choice and why
4. **Alternative**: backup option if the primary does not work
5. **Starting config**: specific hyperparameters to try first
6. **Pitfalls**: what to watch out for with this method
7. **Next step**: the single most important thing to do first

skill-ensemble-builder.md

---
name: skill-ensemble-builder
description: Choose the right ensemble method and configure it for your problem
version: 1.0.0
phase: 2
lesson: 11
tags: [ensemble, bagging, boosting, random-forest, xgboost, stacking]
---

# Ensemble Method Selection Guide

Ensembles combine multiple models to produce better predictions than any single model. The question is always: which kind of ensemble, and when?

## Decision Checklist

1. What is the main problem with your current model?
   - High variance (overfitting): use bagging (Random Forest)
   - High bias (underfitting): use boosting (Gradient Boosting, XGBoost)
   - Both, or you want maximum accuracy: use stacking

2. How much data do you have?
   - Under 1,000 rows: Random Forest (robust, hard to misconfigure)
   - 1,000 to 100,000: XGBoost or LightGBM (best overall for tabular)
   - Over 100,000: LightGBM (fastest gradient boosting, handles large data well)

3. How much tuning time can you invest?
   - Minimal: Random Forest with defaults (almost always works)
   - Moderate: XGBoost with learning_rate=0.1, tune n_estimators with early stopping
   - Maximum: LightGBM or XGBoost with Bayesian hyperparameter search

4. Do you need interpretability?
   - Yes: single decision tree or small Random Forest with feature importance
   - Partial: gradient boosting with SHAP values
   - No: stacking or deep ensembles

5. Is the data noisy with many outliers?
   - Yes: Random Forest (bagging is robust to noise)
   - No: gradient boosting (can push accuracy further on clean data)

## When to use each method

**Random Forest (Bagging)**: your safe first choice. Trains many trees on bootstrap samples and averages. Reduces variance without increasing bias. Nearly impossible to overfit on moderate data. Minimal tuning needed: set n_estimators=100-500 and leave defaults.

**AdaBoost**: sequential boosting with sample reweighting. Works well with simple base learners (decision stumps). Sensitive to outliers and noisy labels because it upweights misclassified points. Largely replaced by gradient boosting in practice.

**Gradient Boosting**: fits each new tree to the residuals of the ensemble so far. Reduces bias. The most powerful method for tabular data. Requires tuning: learning_rate, n_estimators, max_depth, min_child_weight, subsample.

**XGBoost**: gradient boosting with regularization, second-order optimization, and systems-level speedups. Handles missing values natively. The default for Kaggle competitions and production ML on tabular data.

**LightGBM**: gradient boosting with leaf-wise growth (instead of level-wise). Faster than XGBoost on large datasets. Uses histogram-based splits. Best for datasets over 50k rows.

**CatBoost**: gradient boosting with native categorical feature handling. No need to one-hot encode. Good when you have many categorical features.

**Stacking**: trains a meta-learner on the predictions of multiple diverse base models. Use when you need the absolute best accuracy and have compute to spare. Always generate base model predictions via cross-validation to avoid leakage.

**Voting**: simplest ensemble. Hard voting (majority class) or soft voting (average probabilities). Quick way to combine 2-3 diverse models without a meta-learner.

## Common mistakes

- Using gradient boosting without early stopping (it will overfit if you let it run too many rounds)
- Setting learning_rate too high (above 0.3 usually causes instability)
- Not tuning max_depth for gradient boosting (default of unlimited or very deep trees overfit)
- Stacking with models that are all the same type (diversity is the point of stacking)
- Using AdaBoost on noisy data (outliers get higher and higher weight each round)
- Expecting Random Forest to fix underfitting (it reduces variance, not bias)

## Tuning priorities by method

**Random Forest:**
1. n_estimators: 100-500 (more is rarely worse, just slower)
2. max_depth: None (let trees grow fully) or cap at 10-20 for speed
3. max_features: "sqrt" for classification, "log2" or n/3 for regression

**XGBoost / LightGBM:**
1. learning_rate: 0.01-0.3 (lower is better if you have compute for more trees)
2. n_estimators: use early stopping on a validation set instead of guessing
3. max_depth: 3-8 (start with 6)
4. min_child_weight / min_data_in_leaf: 1-20 (higher prevents overfitting)
5. subsample: 0.7-1.0
6. colsample_bytree: 0.7-1.0
7. reg_alpha (L1) and reg_lambda (L2): 0-10

## Quick reference

| Method | Reduces | Speed | Tuning effort | Best for |
|--------|---------|-------|--------------|----------|
| Random Forest | Variance | Fast | Low | Noisy data, quick baseline |
| AdaBoost | Bias | Fast | Low | Simple base learners, clean data |
| Gradient Boosting | Bias | Medium | High | Tabular data, competitions |
| XGBoost | Both | Fast | High | Production tabular ML |
| LightGBM | Both | Fastest | High | Large datasets (50k+ rows) |
| CatBoost | Both | Medium | Medium | Many categorical features |
| Stacking | Both | Slow | High | Maximum accuracy, diverse models |
| Voting | Variance | Fast | None | Quick combination of 2-3 models |