Sub topic 2
Why diversity matters more than count
This chapter assumes you know k-fold cross-validation, the bias-variance trade-off, and basic scikit-learn. It explains why the base learners you pick matter more than how many you pick.
Averaging helps only when errors differ
Suppose you average the predictions of n models. Each prediction has variance σ², and each pair of models has error correlation ρ. The variance of the average is:
ρσ² + (1 − ρ)σ² / n
As n grows, the second term vanishes. What remains is ρσ². If the models make the same mistakes (ρ near 1), adding more of them changes almost nothing. If their errors are weakly related, the average improves a lot.
Ten copies of a random forest with different seeds are highly correlated. One forest, one gradient-boosted model, and one k-nearest-neighbours model usually disagree in useful ways.
Sources of diversity
You can create diversity in several ways. They are listed from strongest to weakest.
- Different algorithm families. Trees, linear models, and distance-based models have different inductive biases.
- Different input representations. Examples are scaled or unscaled features, feature subsets, and engineered features.
- Different hyperparameters. A shallow and a deep tree behave differently.
- Different seeds or bootstrap samples. This is the weakest source, because the models still share an algorithm and the same data.
Measuring diversity
Do not guess. Compute out-of-fold predictions for each candidate model, then check how correlated their errors are.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_predict, train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = make_classification(n_samples=2000, n_features=20, n_informative=8, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
models = {
"rf": RandomForestClassifier(n_estimators=200, random_state=0),
"et": ExtraTreesClassifier(n_estimators=200, random_state=0),
"logreg": make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)),
"knn": make_pipeline(StandardScaler(), KNeighborsClassifier()),
}
# 1 where the out-of-fold prediction is wrong, 0 where it is right.
errors = {
name: (cross_val_predict(model, X_train, y_train, cv=5) != y_train).astype(float)
for name, model in models.items()
}
names = list(errors)
corr = np.corrcoef([errors[n] for n in names])
print("models:", names)
print(np.round(corr, 2))Look at the off-diagonal entries. You should expect rf and et to be strongly correlated, since both are tree ensembles. Pairs that include logreg or knn should show lower correlation. Exact values depend on the data.
Practical consequences
- Prefer three different model families over ten variants of one.
- Drop a model whose errors nearly duplicate another's. It adds cost but little information.
- A weaker model can still help if it is wrong in different places from the strong ones.
- Accuracy still matters. A model that is much worse than the rest can add noise the meta-learner has to filter out.
Exercise
You average 100 models, each with variance σ², and every pair of models has error correlation ρ = 1. What is the variance of the average, and what does this say about adding more models?
Show answer
The variance is ρσ² + (1 − ρ)σ²/n = σ² + 0 = σ². Averaging gives no improvement, because every model makes the same errors. Adding more models cannot help unless they are less correlated.