← 9. Prediction & machine learning
Voting and Stacking
Try this analysis →

Voting and Stacking

Ensemble methods that combine the predictions of different models to predict home sale prices more reliably.

Ensemble learningRegression votingStackingCross-validation

Linear regression$410kRandom forest$430kGradient boosting$425kVoting: average of predictions$421.7k

If, for the same house, linear regression predicts $410,000, a random forest predicts $430,000 and gradient boosting predicts $425,000, which value should you trust? Instead of picking one model, can you use the judgments of several models together?

Methods that combine the results of several predictive models to produce a final prediction are called voting and stacking. Voting averages the models’ predictions, or takes a weighted average, while stacking trains a separate model that takes the base models’ predictions as its input.

Random forests and gradient boosting also use many learners, but what sets voting and stacking apart is that they can combine different kinds of models that have already been built. Still, building an ensemble does not by itself guarantee better performance than the individual models.

They are useful for problems that predict continuous values, such as home prices, sales, demand and customer behavior. In this lesson we use a home sale price example to cover how voting and stacking work, how predictions are combined, preventing data leakage, comparing performance, how they differ from other analyses, and how to report the results.

SECTION 1

What are voting and stacking?

They are widely used ensemble learning methods that combine the predictions of several models.

In regression problems, voting produces the final prediction by taking the average, or a weighted average, of the numbers predicted by several models. Stacking, by contrast, trains a meta-model that uses the predictions made by each base model as new explanatory variables. To distinguish it from majority voting in classification, regression voting is usually described as averaging.

SECTION 2

Why do we need voting and stacking?

They are used to combine the strengths and weaknesses of different models.

Linear regression expresses linear relationships between variables fairly clearly but can miss complex nonlinearity. A random forest reduces variability by averaging many trees, and gradient boosting corrects the errors of earlier models one after another. When different errors are mixed together, an ensemble may reduce the error, but if the models are wrong in the same direction, the benefit is limited.

Key point: more models is not what makes an ensemble good; an ensemble is valuable when its predictions complement one another and the performance holds up on new data.
SECTION 3

How do voting and stacking work?

Voting takes an average, while stacking learns a rule for combining the predictions.

Combining several models’ predictions to produce a final priceExample predictions for the same house · Unit: $ thousandsLinear regression$410kRandom forest$430kGradient boosting$425kVoting: average of predictions$421.7k
An example of regression voting that takes a simple average of three models’ home price predictions. The numbers are hypothetical values for illustration.

① Regression voting: average the predictions

If the models predict 410, 430 and 425 ($ thousands), the simple average is about 421.7 ($ thousands). You can also use a weighted average that gives more weight to better models, but the weights must be set during training without looking at the evaluation data.

② Stacking: learn how to combine

In stacking, the predictions of the three base models are used as inputs to a meta-model. The meta-model can learn in which situations to trust which model’s predictions more.

Stacking learns from the models’ predictionsOOF predictions on the training data → meta-model → prediction for a new houseLevel-1 models3 base modelsOOF predictionsCross-validation resultsMeta-modelTakes predictions as inputNew house: base model predictions → trained meta-model → final priceThe meta-model is not trained on the final evaluation data.
A mock-up of how stacking works. The meta-model is trained on out-of-fold (OOF) predictions obtained through cross-validation.

③ Don’t reselect models during the final evaluation

Decide on the base models, the combining rule and the hyperparameters within the training data, and then check performance just once at the end on independent evaluation data. Repeatedly looking at the evaluation score and making adjustments can make the results overly optimistic.

SECTION 4

A real-world example: predicting the sale prices of home listings

We compare three base models and two ensembles on the same housing data.

We prepared 1,000 hypothetical home listings. The sale price is predicted from floor area, number of rooms, year built, neighborhood, distance to a transit station, and so on. 800 listings are used for training and cross-validation, and 200 are set aside for the final evaluation.

VariableExampleRole
Sale price$425,000Target variable
Floor area1,850 ft²Input variable
Rooms3Input variable
Year built2008Input variable
NeighborhoodA / B / CCategorical input variable
Distance to transit station1.2 milesInput variable

The values in the results table are hypothetical numbers designed to explain the method. They should not be interpreted as performance obtained by actually training and evaluating models.

SECTION 5

What should you check before the analysis?

First check the characteristics of the price data and the evaluation design.

ItemWhat to check
Distribution of sale pricesInfluence of high-priced homes and unusual sales
Missing variablesWhether key information such as floor area, age or neighborhood is missing
TimingIf predicting future prices, split in time order
Duplicate listingsThat the same house is not in both the training and evaluation data
Preprocessing leakageFit imputation, standardization and encoding on the training data only
Evaluation metricsCheck errors with MAE, RMSE and in actual dollar amounts
SECTION 6

In what order are the models trained?

Build the base models first, then set up voting and stacking.

① Split off the training data and the final evaluation data

Model development is carried out only on the 800 listings, excluding the 200 held out for the final evaluation. If the actual sale dates need to be taken into account, a time-based split may be more appropriate than a random split.

② Train the three base models

Compare linear regression, a random forest and gradient boosting on the same training data, and choose their settings through cross-validation.

③ Set up the voting model

Take a simple average of the base models’ predictions, or combine them with weights chosen from cross-validation results on the training data.

④ Train the stacking meta-model

Collect the predictions (OOF) for each training row from base models trained on the folds that exclude that row. Training the meta-model on these predictions ensures it does not simply use the overly good predictions of models that were trained on those same rows.

⑤ Compare performance on the same evaluation data

Apply the five models that have completed the selection process to the 200 listings in the final evaluation data and compare their prediction errors.

SECTION 7

How are voting weights chosen?

Start with a simple average, and choose any weighted average using validation data.

ApproachHow it combinesFeatures
Simple-average votingAverages the three predictions 1:1:1Easy to set up and a good baseline for comparison
Weighted-average votingE.g., averages with weights 0.2·0.3·0.5Weights must be chosen during validation
StackingTrains a meta-model on the predictionsAllows even nonlinear combinations, but adds complexity

A model with a large weight is not necessarily the better model. The correlation of errors between models also affects the combined result. With real data, it is best to check the simple average first and then compare more complex methods.

SECTION 8

How do you compare predictive performance?

Use the same final evaluation data and the same error metrics.

ModelMAE ($ thousands)RMSE ($ thousands)
Linear regression4255
Random forest3142
Gradient boosting2939
Voting2736
Stacking2635
Prediction errors on the final evaluation dataMAE · lower is better · hypothetical example · Unit: $ thousandsLinear regression42Random forest31Gradient boosting29Voting27Stacking26
A mock-up comparing mean absolute error (MAE). All numbers are hypothetical; lower values mean smaller prediction errors.

In the hypothetical results, stacking has the lowest MAE of the five models, at $26,000. But it differs from voting’s $27,000 by only $1,000. Further review is needed to tell whether this difference is due to the particular evaluation sample, and whether it matters enough in practice to justify the added model complexity.

SECTION 9

How do you interpret MAE and RMSE?

For both metrics smaller is better, but they differ in how much weight they give to large errors.

MAE is the average of the absolute differences between the predicted prices and the actual sale prices. An MAE of $26,000 means the average absolute error in the evaluation data was about that much. RMSE gives more weight to large errors, so it helps you see whether the price was badly off for some houses.

When interpreting the results: an MAE of $26 thousand does not mean every house is always predicted to within $26 thousand. You should also check how the errors are distributed by neighborhood and price range.
SECTION 10

Things to watch out for with voting and stacking

A complex combination does not automatically guarantee better predictions.

Are the base models’ errors too similar?

If the three models are wrong in similar ways for the same houses, averaging them may improve things only a little. Check the correlations between their predictions and residuals.

Is there data leakage in the stacking?

If the meta-model is trained on the base models’ direct predictions on their own training data, overfitting is likely. Use OOF predictions, and fit all preprocessing within the training folds of the cross-validation.

Has performance improved enough to justify the complexity?

If the improvement is small, you might choose a simpler model or voting, taking into account training and operating costs, explainability and inference latency.

Does it generalize across neighborhoods and time periods?

If listings from the same nearby development or sales from adjacent time periods end up in both datasets, performance can be rated higher than it would be for genuinely new listings.

SECTION 11

How does it differ from other analyses?

Even when several learners are used, the ways they are combined differ.

MethodHow it trains and combinesFeatures
VotingAverages the final predictions of separate modelsSimple combination
StackingTrains a meta-model on the base models’ predictionsOOF predictions are essential
Bagging · random forestCombines many models built with resampling or randomizationFocuses on reducing variance
Gradient boostingTrains sequentially, correcting the loss of earlier learnersImproves step by step
Single linear regressionFits a single linear relationshipCoefficients relatively easy to interpret
SECTION 12

How do you report the results?

Present the data split, the combining method and the error metrics together.

In the report, state the sample size, the input variables, how the data were split, how missing values and categorical variables were handled, the settings of each model, the voting weights, the fold setup and meta-model for stacking, the evaluation metrics, and the units of the home prices.

Sample write-up

Of 1,000 hypothetical home sales, 800 were used for model training and cross-validation and 200 for the final evaluation. Linear regression, a random forest and gradient boosting were used as base models, and we compared voting, which took a simple average of their predictions, with stacking, which trained a meta-model on cross-validated OOF predictions. On the final evaluation data, the MAE was $42 thousand for linear regression, $31 thousand for the random forest, $29 thousand for gradient boosting, $27 thousand for voting and $26 thousand for stacking. The numbers shown are hypothetical examples set up for illustration.

SECTION 13

Where can voting and stacking be used?

They can be used when different types of predictive models capture different patterns.

FieldWhat is predictedExample base models
Real estateHome sale pricesLinear regression · tree-based models
RetailWeekly sales volumeRegression · boosting models
EnergyElectricity demandTime series · machine learning regression
ManufacturingProduct quality scoresRegularized regression · random forest
FinancePredicting loss amountsRegression · boosting models

Stacking is not the answer in every field. What matters is confirming the improvement through independent evaluation and judging whether the complexity is manageable in operation.

✓ Wrap-up

Voting and stacking pool the predictions of different models to try for better results.

What matters is not how elaborate the combination is, but whether the prediction error for new houses actually went down.


In the home sale price example, linear regression, a random forest and gradient boosting predicted different prices for the same listing. Voting averaged the three values to produce a final price, and stacking additionally learned a combining rule from the base models’ predictions. In the hypothetical results stacking had the lowest MAE, but we cannot assume that the performance differences between the individual models and the ensembles would be reproduced with real data.

For stacking in particular, a design that prevents data leakage while training the meta-model is essential. Rather than feeding the meta-model direct predictions on the training rows, use cross-validated OOF predictions. The final evaluation data should not be used repeatedly to choose models or tune weights. If you consider maintenance costs and explainability as well as predictive accuracy, then when the improvement is small, simple voting or a single model that works well may be more appropriate.

KEY TAKEAWAY

Regression voting averages the price predictions of several models, while stacking trains a meta-model that uses the base models’ predictions as input. The stacking meta-model should be trained on OOF predictions obtained through cross-validation, and the final evaluation data must be kept separate. The value of an ensemble is judged not by the number of models, but by the reduction in prediction error confirmed on independent evaluation data and by whether it is practical to operate.