Ensemble Methods
Bagging vs boosting vs stacking, diversity principle
In 2006 Netflix offered a one-million-dollar prize to anyone who could beat their movie-recommendation system by 10%. Teams around the world chased it for three years. And the winning entry, when it finally crossed the line, was not one brilliant model — it was a blend of dozens of different models mashed together. That was the lesson the whole field took away: a crowd of different models, combined, beats any single model, even the best one. That is ensembling.
Here it is in miniature. A bank wants to predict loan default. A decision tree scores 76%. Logistic regression: 78%. A random forest: 84%. Good, not great. Then they take all three and simply let them vote on each applicant. The combination scores 86% — higher than the best single model in the mix. How do three so-so models add up to something better than the best of them?
Why combining works — and its one condition
The trick is that the models are wrong in *different places*. The tree blunders on some applicants; logistic regression happens to get those right; the forest covers a third set. When they vote, each model's mistakes are outvoted by the other two, while their correct answers pile up and agree. The errors cancel; the truth reinforces.
But here is the whole secret: this only works if the models make *different* mistakes. If all three fail on exactly the same applicants, voting changes nothing — you have just repeated one opinion three times. So the single thing that makes an ensemble strong is diversity: models whose errors are unrelated. Two mediocre models that fail in different places beat two excellent models that fail in the same places. Diversity, not raw accuracy, is the lever.
Three ways to build a diverse crowd
There are three classic recipes, and you have already met two of them.
Bagging builds diversity through data: train each model on a different random resample of the rows, then average. That is exactly what a random forest does, and it mainly cuts *variance* (the twitchiness).
Boosting builds diversity through sequence: train models one after another, each focused on the mistakes the team has made so far. That is gradient boosting, and it mainly cuts *bias* (the systematic miss).
Stacking is the most general and the most powerful. Train several genuinely different models — a tree, a linear model, maybe a neural net — then train one more small model, a meta-learner, whose only job is to learn *how much to trust each model in which situation*. It might learn "trust the boosting model for high-income applicants, but lean on logistic regression for people with thin credit files." It learns to combine, rather than just averaging.
The one trap that quietly ruins stacking
Stacking has a subtle failure mode you have to design around. To train the meta-learner, you feed it the base models' predictions. But if you let each base model predict on the very rows it was trained on, those predictions are dishonestly good — the models have partly *memorised* those rows, so their outputs look far more reliable than they will be on fresh data. The meta-learner then learns to trust a signal that vanishes at test time, and the whole stack falls apart in production.
The fix is out-of-fold predictions: split the data into folds, train each base model on some folds, and have it predict only on the folds it did *not* see. Those held-out predictions are honest — they show what the base models can really do on unseen rows — so the meta-learner learns from the truth instead of a memory.
The belief to drop
"More models always help." No — *diversity* helps. Bolt a fifth copy of the same random forest onto your ensemble and you gain essentially nothing; it makes the same mistakes as the other four, so the vote does not budge. But add a model built on a *different* idea — a linear model beside your trees — and even if it is individually weaker, it can lift the whole ensemble, because it fails in places the others do not. When an ensemble stops improving, do not add more of the same. Add something different.
Hard votes versus soft votes
There are two ways to let classifiers vote. Hard voting counts labels — majority wins. Soft voting averages the predicted *probabilities* and then thresholds, which usually works better because a model that's 0.9 sure should outweigh one that's barely 0.51. But soft voting has a prerequisite that trips people up: it only makes sense if the base models are calibrated. If one model is systematically overconfident, its inflated probabilities dominate the average and drag the ensemble toward its mistakes. So calibrate the base models (or at least check them) before averaging probabilities — otherwise soft voting can underperform plain hard voting.
Blending versus stacking
Two ways to make the honest meta-features, and interviewers like the distinction. Stacking uses K-fold out-of-fold predictions: every row gets a prediction from a base model that didn't train on it, so you use all the data for both levels. Blending is the simpler cousin — hold out a single validation set, train base models on the rest, and let them predict once on that holdout to build meta-features. Blending is easier and has zero fold-leakage risk, but it "wastes" the holdout (base models never train on it) and gives the meta-learner less data. Stacking is more data-efficient but must handle folds carefully.
Keep the meta-learner simple
The meta-learner's input is just a handful of base-model predictions, so it needs almost no capacity. A regularised logistic regression (or plain linear model) is the standard choice, and for good reason: a complex meta-learner (another boosting model on top) easily *overfits* the base predictions, especially since those predictions are highly correlated. Simple meta-learner, honest out-of-fold features — that's the reliable recipe.
Out-of-fold must respect time and groups
The OOF trick assumes rows are independent — and it leaks exactly like OOB does when they aren't. For time-series data, a random fold lets a base model see the future when generating a "held-out" prediction for the past, so the meta-features are leaked; you need time-ordered folds. For grouped data (many rows per user), all of a user's rows must fall in the same fold, or a base model trained on some of a user's rows predicts the rest. Get this wrong and the stack looks brilliant offline and collapses in production — the ensemble version of the same leakage that haunts every model here.
Ensembles aren't free
The accuracy comes with real costs worth naming: every base model must run at inference, so latency and memory multiply; retraining and deployment get more complex; debugging a wrong prediction across five models is far harder than for one; and interpretability drops sharply. For a point or two of accuracy you may pay 5× the serving cost — sometimes worth it (a fraud model, a Kaggle prize), often not (a latency-bound real-time system). That's why production frequently ships a single boosted model or *distills* the ensemble into one smaller model.
Where diversity actually comes from
Diversity isn't only "different algorithms." You can manufacture it from different feature subsets, different training samples (bagging), different loss functions, different random seeds, different hyperparameters, different time windows, and even different target definitions. The most robust ensembles combine several of these axes at once — a tree and a linear model, on different feature sets, with different seeds — because the more *independent* the sources of disagreement, the better the errors cancel.
Key points
- Reach for stacking when you already have several different models and enough data to make out-of-fold predictions. It almost always beats any single model by a point or two, because a small meta-learner can work out which base model to trust where — leaning on boosting for one kind of case and a linear model for another. The cost is training time and a bit of plumbing (the out-of-fold step). For a quick win without the plumbing, even a plain average of a few diverse models' probabilities usually edges out the best one on its own.
- The trap that quietly ruins a stack: feeding the meta-learner predictions the base models made on their own training rows. A base model partly memorises the rows it trained on, so its predictions there look far better than they will be on new data. Train the meta-learner on those and it learns to trust a signal that disappears at test time. Always build the meta-features from out-of-fold predictions — each base model predicts only on rows it did not train on. In scikit-learn, cross_val_predict gives you these in a single call.
- The check: look at whether your models actually make different mistakes. For each model, mark which examples it got wrong on a held-out set, then compare those error patterns across models. If two models are wrong on almost exactly the same examples, they are effectively one model for ensemble purposes and combining them buys nothing. When errors are that correlated, do not add another similar model — add one built on a different idea (a different algorithm, or different features), which is the only thing that will actually move the ensemble.
- Soft voting beats hard voting only if the base models are calibrated, and blending and stacking make honest meta-features differently. Hard voting counts labels; soft voting averages probabilities (usually better) but is dominated by an overconfident model unless the bases are calibrated first. Stacking builds meta-features from K-fold out-of-fold predictions (data-efficient); blending uses a single holdout (simpler, no fold-leakage, but wastes data). Keep the meta-learner simple — a regularised logistic/linear model — since a complex one overfits the correlated base predictions.
- Respect time/group boundaries in OOF, and weigh the real cost of ensembling. Out-of-fold prediction leaks exactly like OOB when rows aren't independent: use time-ordered folds for temporal data and keep each group (all of a user's rows) in one fold, or the stack looks great offline and dies in production. And ensembles aren't free — every base model runs at inference, so latency, memory, retraining complexity, debugging difficulty, and opacity all multiply for a point or two of accuracy, which is why production often ships one boosted model or distills the ensemble. Manufacture diversity from many axes: algorithms, feature subsets, samples, losses, seeds, hyperparameters, time windows, target definitions.
An ensemble combines several models and beats the best single one — but only because they make different mistakes, so voting cancels the errors. Diversity, not the number of models, is the lever: bagging builds it from different data, boosting from fixing mistakes in sequence, and stacking by training a meta-learner to combine genuinely different models — using out-of-fold predictions, or the whole thing leaks.
Recap
- Ensemble = combine several models, beat the best single one — because they make different mistakes and voting cancels errors.
- Diversity, not count, is the lever.
- Bagging builds diversity from different data; boosting from fixing mistakes in sequence; stacking trains a meta-learner over different models.
- Stacking must use out-of-fold predictions — feed the meta-learner base predictions on their own training rows and it leaks.
- Soft voting beats hard voting only if base models are calibrated.
- Respect time/group boundaries in OOF, and weigh the real cost of ensembling.
Check your understanding
Q1. Your stacking ensemble looks great in training but flops on validation. The base models' predictions on their own training rows were used as the meta-learner's inputs. What went wrong, and what is the fix?
- `A) The meta-learner is too complex and simply memorised the base models' outputs; swap it for a plain unregularised linear model to close the gap.`
- `B) The base models are too similar, so correlated outputs confuse the meta-learner into chasing spurious patterns; drop all but one of them entirely.`
- `C) Leakage: base models partly memorised their own rows, so those predictions look too good. Fix with out-of-fold predictions.`
- `D) The training set is too big for the meta-learner, finding patterns that don't generalise; subsample to roughly ten times the base-model count.`
Q2. Three so-so models combine into an ensemble that beats the best of them. What makes that possible?
- `A) The ensemble quietly picks whichever single model is best on each example and copies its answer, so it never does worse than the strongest member.`
- `B) The models are wrong in different places, so voting outvotes each one's mistakes while correct answers agree; this fails when errors are correlated.`
- `C) Averaging always beats any single model mathematically regardless of which ones are involved, reducing error by a factor equal to the model count.`
- `D) Each model corrects the previous one's leftover errors in turn, so mistakes shrink step by step, which is why chained weak models beat the strongest one.`
Q3. Two models are wrong on exactly the same examples. Select the two true statements about what combining them gets you.
- `A) Nothing — with identical errors there is nothing to cancel, so the ensemble scores about the same as either model would alone on its own.`
- `B) It shows diversity, not model count, is what makes ensembles work: unrelated errors cancel out through voting, while identical ones simply do not.`
- `C) A big jump — combining two models always multiplies their strengths, so even identical-error models land comfortably above either one alone.`
- `D) The higher of the two accuracies, since probability averaging amplifies the more confident model's correct answers and discards the weaker model's mistakes.`
Q4. You switch a voting ensemble from hard voting to soft voting (averaging probabilities) and it gets worse. One base model is badly overconfident. Why did soft voting hurt?
- `A) Soft voting is always worse than hard voting because averaging probabilities discards the majority signal entirely; revert to hard voting as a rule.`
- `B) An uncalibrated, overconfident model pushes extreme probability values that dominate the average, dragging the ensemble toward its own mistakes.`
- `C) The overconfident model simply has too few trees, so its probabilities are noisy; adding more trees to just that model fixes soft voting entirely.`
- `D) Soft voting requires all base models to share the same algorithm; mixing a tree with a linear model is what actually broke it, not calibration.`
Q5. You're stacking models on time-ordered transaction data and generate out-of-fold meta-features with a plain random K-fold split. What's the risk?
- `A) No risk — out-of-fold predictions are leak-proof by construction, so a random split is always safe for stacking regardless of data type.`
- `B) The only risk is slower training; random folds are statistically fine for time-series stacking, just less compute-efficient than time-ordered ones.`
- `C) A random fold lets a base model train on future rows and predict a past row, leaking the future into meta-features; use time-ordered folds instead.`
- `D) The risk is the meta-learner overfits, fixed by using a more powerful meta-learner such as a boosting model stacked on the base predictions.`
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →