Lesson 1 of 9
Ensemble Learning Theory: Bias Reduction vs Variance Reduction
Ensemble learning combines the predictions of multiple individual base estimators ('weak learners') to construct a single superior meta-model. Ensemble theory is fundamentally split into two mathematical paradigms: Bagging (Bootstrap Aggregation), which trains independent base learners in parallel on resampled datasets to reduce model Variance; and Boosting, which trains base learners sequentially, where each new learner corrects the pseudo-residuals of the prior ensemble to reduce model Bias.
Simulating Bagging (Variance Reduction) vs Boosting (Bias Reduction)
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import BaggingRegressor, GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
# Generate non-linear sinusoidal function corrupted with noise
np.random.seed(42)
n = 300
X = np.sort(np.random.uniform(0, 10, n)).reshape(-1, 1)
y = np.sin(X.ravel()) * 5.0 + np.random.normal(0, 1.2, n)
# 1. BASE LEARNER 1: Deep Overfitting Tree (Low Bias, High Variance)
deep_tree = DecisionTreeRegressor(max_depth=12, random_state=42)
deep_tree.fit(X, y)
# BAGGING OVER DEEP TREES: Slashes Variance
# Var(mean(f_i)) = (1/B) * Var(f) + ((B-1)/B) * Cov(f_i, f_j)
bagging_ensemble = BaggingRegressor(
estimator=DecisionTreeRegressor(max_depth=12),
n_estimators=50,
random_state=42
)
bagging_ensemble.fit(X, y)
# 2. BASE LEARNER 2: Shallow Underfitting Stump (High Bias, Low Variance)
shallow_stump = DecisionTreeRegressor(max_depth=1, random_state=42)
shallow_stump.fit(X, y)
# BOOSTING OVER SHALLOW STUMPS: Slashes Bias sequentially
boosting_ensemble = GradientBoostingRegressor(
max_depth=1, # Uses weak stumps
n_estimators=50,
learning_rate=0.2,
random_state=42
)
boosting_ensemble.fit(X, y)
# Compare MSE
print("=== ENSEMBLE PARADIGM COMPARISON ===")
print(f"Deep Tree (Overfit) MSE: {mean_squared_error(y, deep_tree.predict(X)):.4f}")
print(f"Bagging Ensemble (50 trees)MSE: {mean_squared_error(y, bagging_ensemble.predict(X)):.4f} (Variance successfully tamed!)")
print(f"Shallow Stump (Underfit) MSE: {mean_squared_error(y, shallow_stump.predict(X)):.4f}")
print(f"Boosting Ensemble (50 trees)MSE: {mean_squared_error(y, boosting_ensemble.predict(X)):.4f} (Bias systematically eliminated!)")Pro Tip: The Independence Prerequisite for Bagging
Bagging only reduces variance if the individual base models make uncorrelated errors: `Var_ens = rho * Var + (1 - rho)/B * Var`. If all 100 trees in an ensemble make identical errors (`rho = 1`), bagging reduces variance by 0%. Decorrelating trees via random feature subsampling (Random Forests) is mathematically necessary.
MlEnsembleMethods Visual Representation
Hands-On Challenge: Build a Manual Voting Classifier
Write a `ManualSoftVotingClassifier` that takes fitted instances of LogisticRegression, DecisionTree, and SVC, calculates the arithmetic average of their `predict_proba` matrices, and assigns class labels based on argmax. Verify that the ensemble outperforms any single constituent model.
Quick Quiz & Key Concept Checks
Common Pitfall: Ensembling Identical Highly Correlated Models
Averaging the predictions of 10 identical models trained with different random seeds will yield almost zero performance improvement because their prediction errors are 99% correlated. Effective ensembles require diversity across algorithms, feature subsets, or loss objectives.
Finished this lesson?