Blending and stacking

Combine different models by voting, averaging, or by training a meta-model on their out-of-fold predictions.

Lesson 47 of 6614 minAdvanced

You will learn to

  • Blend models with soft, hard or average voting
  • Stack models with a meta-learner trained on out-of-fold predictions
  • Avoid stacking leakage

Before this lesson

Intuition

A single model has blind spots. A linear model misses curves, a tree misses smooth trends, a nearest-neighbour model struggles when there are many irrelevant columns. Models from different families tend to make different mistakes, and that is exactly what an ensemble needs.

There are two common ways to combine them:

  • Blending (voting): let the models vote, or average their numbers. Simple, with nothing new to train.
  • Stacking: train a second model, the meta-learner, whose inputs are the predictions of the first-level models. It learns how much to trust each one, and in which situations.

A committee analogy: blending is a show of hands where every expert counts the same. Stacking is a chairperson who has watched the experts over many past decisions and knows that the cardiologist is reliable for heart questions and unreliable for the rest.

Picture it

BlendStackModel 1Model 1Model 2Model 2Model 3Model 3VoteyOut-of-foldpredictionsMetalearnerVote -> PredictionMeta-learner -> Prediction
Left: a blend combines the base models' outputs directly. Right: a stack feeds out-of-fold predictions of the same base models to a meta-learner, which produces the final prediction.

The dashed box on the right is the important part. The meta-learner never sees predictions that a base model made on rows it had been trained on.

The math

Blending, classification. For models m=1,…,Mm = 1, \dots, M with predicted probabilities pm(x)p_m(x) of the positive class:

  • Soft voting averages the probabilities: pˉ(x)=1M∑mpm(x)\bar{p}(x) = \frac{1}{M}\sum_m p_m(x), and predicts the positive class if pˉ>0.5\bar{p} > 0.5.
  • Hard voting counts votes: each model predicts the class with probability above 0.5, and the majority wins.

Soft voting needs models that output probabilities. Blending, regression is plain averaging of the predictions.

Stacking. Split the training rows into KK folds. For each fold, train every base model on the other K−1K-1 folds and predict the held-out fold. Collected over all folds, each row gets one out-of-fold prediction from each base model: a matrix with one column per model. The meta-learner gg is fitted on that matrix:

y^(x)=g(f^1(x),…,f^M(x))\hat{y}(x) = g\big(\hat{f}_1(x), \dots, \hat{f}_M(x)\big)

At prediction time, the base models are refitted on all the training rows and their outputs go into gg.

Why out of fold. On its own training rows, a flexible base model is nearly perfect. Fitting the meta-learner on those predictions would teach it that this model is nearly always right, and it would then fail on new rows. This is a form of leakage.

Worked example

Blending. Take three classifiers and two rows:

RowModel 1Model 2Model 3Hard voteSoft meanSoft prediction
A0.800.400.6010.6001
B0.550.550.0510.3830

Row A: both methods say class 1. Row B: two models lean slightly positive and one is very sure of negative. Hard voting counts two votes for class 1. Soft voting keeps the confidence of model 3 and ends at 0.383, so it says class 0. When models are well calibrated, soft voting uses more information.

Stacking, one prediction. Suppose the meta-learner is a logistic regression with these illustrative coefficients: intercept -1.2, and weights 2.0, 0.5 and 1.5 for the three models. For row A (0.80, 0.40, 0.60):

z=−1.2+2.0(0.80)+0.5(0.40)+1.5(0.60)=1.5,p=11+e−1.5=0.818z = -1.2 + 2.0(0.80) + 0.5(0.40) + 1.5(0.60) = 1.5, \qquad p = \frac{1}{1 + e^{-1.5}} = 0.818

The meta-learner trusts model 1 most (weight 2.0) and model 2 least (0.5). With row B (0.55, 0.55, 0.05): z = -1.2 + 1.1 + 0.275 + 0.075 = 0.25, so p = 0.562, class 1. The stack weighs the models unevenly, so it can differ from a plain blend. The weights are made up to show the arithmetic; in a real run the meta-learner learns them from the out-of-fold matrix.

How mlflow does it

  • On a finished run, select 2 to 10 models on the leaderboard. An action bar offers Stack, Blend and Combine…. The ensemble trains as a new model in the same run, with the same data split and cross-validation folds as its members, so the scores are comparable. Blends and stacks cannot be nested.
  • Blend, classification. The Voting method is Auto (soft if every selected model supports probabilities, otherwise hard), Soft or Hard. Soft is blocked with a message when a selected model has no class probabilities.
  • Blend, regression. Averaging.
  • Stack. Pick a Meta-learner. For classification: Logistic Regression (default), Random Forest, Decision Tree. For regression: Linear Regression (default), Ridge Regression, Random Forest. The screen notes that linear models usually meta-learn best and tree-based meta-learners can overfit on small leaderboards.
  • Blend and stack are available for classification and regression runs. If the ensemble fails, the run stays finished and only that model shows the error.
  • The ensemble's model page compares its score with each member's, so you can see whether it beat the best base model.

See Improve a model.

Common mistakes

  • Stacking on in-sample predictions. Always use out-of-fold predictions for the meta-learner. Using fitted-model predictions on training rows makes the stack over-trust overfitted members.
  • A flexible meta-learner on few inputs. Start with the linear default. Switch to a forest only with plenty of rows, and check it on held-out data.
  • Blending models of one family. Ten gradient boosting variants are close to identical. Choose diverse members.
  • Not comparing with the best single model. If the ensemble does not beat the best member by more than the fold-to-fold spread, keep the single model.
  • Ignoring serving cost. A stack contains every base model plus the meta-learner. Prediction time is the sum of all of them.

Try it in mlflow

Blend or stack from the leaderboard

  1. Open Runs and open a finished classification run.
  2. On the leaderboard, select three models from different algorithm families.
  3. Click Blend, keep the Auto method and click Start blending.
  4. Select the same models again, click Stack, keep the default Meta-learner and click Start stacking, then compare the three scores.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. Three classifiers output the probability of class 1: 0.55, 0.55 and 0.05. What do hard voting and soft voting predict, with a 0.5 threshold?
  2. 2. Why must a stacking meta-learner be trained on out-of-fold predictions?
  3. 3. You stack five models and the meta-learner is a deep random forest on a small leaderboard. What is the main risk?