Why ensembles work
Averaging models that make different mistakes reduces error, and the size of the gain depends on how different those mistakes are.
Lesson 44 of 6610 minIntermediate
You will learn to
- Explain error reduction from combining diverse models
- Tell variance reduction from bias reduction
- Know when an ensemble is not worth it
Before this lesson
Intuition
Ask one friend to guess how many sweets are in a jar and the answer may be far off. Ask fifty friends and average the guesses, and the result is usually much closer. Some guesses are too high and some too low; when you average them, many of those errors cancel.
An ensemble does the same with models. You train several models and combine their predictions into one. The trick is in the word "different". If all your friends look at the jar from the same spot and copy each other, averaging adds nothing. The gain comes from models whose mistakes are not the same mistakes.
Two kinds of error matter here, from the bias-variance lesson:
- Variance is how much a model changes when the training sample changes. Averaging many models trained on different samples smooths that out. This is the idea behind bagging.
- Bias is a systematic miss. Averaging models that all share the same bias keeps the bias. To lower bias you need models that build on each other and fix what the earlier ones missed. This is the idea behind boosting.
Picture it
Each grey curve is a flexible model that followed the noise in its own training sample, in its own way. The wobbles point in different directions, so they largely cancel in the average. If every grey curve had the same wobble, the blue line would have it too.
The math
Suppose each of models makes a prediction with variance , and the predictions of any two models have correlation . The variance of the average prediction is:
Read the two terms:
- shrinks as you add models. This is the part an ensemble can remove.
- does not depend on . It is the floor. The more correlated the models, the higher it is.
So there are two levers: add models, and make them less alike. After a handful of models the second lever matters more, because the first term is already small.
The average has the same bias as the typical member, since is the average of the members' expected predictions. That is why averaging attacks variance and not bias.
For classification by majority vote the same idea appears as a probability. If each of an odd number of independent classifiers is correct with probability , the vote is correct more often than any single one.
Worked example
Take five models with variance (the unit is arbitrary), and compare three levels of correlation. The numbers are computed from the formula above, with :
| Correlation between models | Variance of the average |
|---|---|
| 0 (independent) | 0 + 1 / 5 = 0.20 |
| 0.3 | 0.30 + 0.7 / 5 = 0.44 |
| 0.9 (near copies) | 0.90 + 0.1 / 5 = 0.92 |
Five independent models cut the variance by a factor of five. Five models that mostly agree cut it by only 8%. That is why a blend of a linear model, a tree model and a nearest-neighbour model often beats five copies of the same tree model with different seeds.
A voting example makes the same point. Five independent classifiers, each right 70% of the time, vote. The majority is right when at least three are right:
An accuracy of 83.7% from five 70% models. Real classifiers are never fully independent, so the real gain is smaller, but the direction holds. If the five models are correlated, the number falls toward 70%.
How mlflow does it
- On a finished run, you can select two to ten succeeded models on the leaderboard and combine them as a Blend or a Stack. The ensemble trains as a new model in the same run, so it appears next to its members and you can compare scores directly.
- On a single model's page, Improve then Ensemble wraps that one model in Bagging or Boosting. The hint on the screen says it briefly: bagging reduces variance, boosting reduces bias.
- Ensembles are available for classification and regression runs.
See Improve a model for the screens.
Common mistakes
- Averaging near-identical models. Five gradient boosting models with slightly different settings are highly correlated, so the gain is small. Mix algorithm families instead.
- Expecting an ensemble to fix underfitting. If every member is too simple, the average is too simple. Add model flexibility or better features first.
- Adding weak members. One model that is far worse than the rest can drag a plain average down. Combine models that are all competitive on the leaderboard.
- Ignoring the cost. An ensemble of ten models is ten times as large to store and slower to serve. Check that the gain exceeds the fold-to-fold spread of the score before paying for it.
- Judging the ensemble on data its members saw. Compare the ensemble and its members on the same cross-validated or hold-out data, never on training rows.
Try it in mlflow
Select candidates for an ensemble
- Open Runs and open a finished classification or regression run.
- On the leaderboard, tick two or more models that use different algorithms.
- Look at the ensemble bar that appears: it offers Blend, Stack and Combine.
- Do not start it yet: first note each model's score, so you can compare the ensemble against its members later.