Stacking and Voting Ensembles

Combining different models rather than many copies of one, and the case where a majority vote makes things worse.

Stacking and Voting Ensembles

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Voting combines predictions by majority, or by averaging probabilities. Every model counts equally.
Stacking trains a second model — the meta-learner — on the base models' predictions, so it can weight them.
Bagging and boosting combine many copies of one model. Voting and stacking combine different models.
An ensemble helps when the members make different mistakes. Three models that fail on the same rows gain nothing.

Overview

An ensemble combines predictions from several models. Voting uses a fixed rule such as majority vote or mean probability. Stacking trains a meta-model to combine base-model predictions, which can learn when one model is more reliable than another.

In this guide
  1. Voting: combine predictions directly
  2. Stacking: learn the combination
  3. Explore how models combine
  4. Choosing among the approaches
  5. Sources and next steps

Voting: combine predictions directly

In hard voting, each classifier casts a class vote and the majority wins. In soft voting, the classifier probabilities are averaged, then the largest average determines the class. Soft voting can use confidence information, but it assumes those probabilities are meaningfully comparable; calibration may matter.

Voting helps when models make complementary errors. If every model makes the same wrong predictions, a vote cannot correct them. Adding a weak or highly correlated model can leave the result unchanged or make it worse.

Stacking: learn the combination

Stacking treats base-model predictions as features for a second model, called the meta-learner. For example, the meta-learner might learn to trust a linear classifier in one region and a tree in another.

The meta-learner must be trained on out-of-fold predictions. Split the training data into folds; for each fold, fit each base model on the other folds and predict the held-out fold. Combine those held-out predictions to train the meta-learner. This prevents the meta-model from learning from base-model predictions made on examples the base models already fitted. Afterward, refit base models on all training data for prediction. Use a separate validation or test procedure to evaluate the entire stack.

Explore how models combine

The visualiser shows three base classifiers with different decision boundaries. Adjust Stump on a: threshold, Stump on b: threshold, and Radius rule: threshold to see how their errors overlap. A useful combination needs models that are individually informative and do not fail on exactly the same cases.

Compare the combined result with its strongest single model. If the ensemble does not improve the relevant validation metric enough to justify added cost and complexity, keep the simpler model.

Choosing among the approaches

MethodHow it combinesMain requirement
Hard votingMajority class voteBase models should make useful, complementary predictions.
Soft votingMean of class probabilitiesScores should be comparable; check calibration and tune weights on validation data.
StackingA learned meta-model over base predictionsTrain on out-of-fold predictions and validate the whole pipeline.

Ensembling can improve accuracy, but it increases training, serving and debugging costs. The best score on the training data is not evidence of better generalisation. Use cross-validation for model selection and keep the test set for the final comparison.

Sources and next steps

Scikit-learn documents voting classifiers and stacking classifiers. Continue with random forests for an ensemble whose trees are trained as a single method.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why can a majority vote score worse than its best member?

  2. Why must the meta-learner be trained on cross-validated predictions?

  3. What condition must hold for an ensemble to help at all?

Cheat sheet

Stacking and Voting Ensembles

Combine trees, linear models and neighbours into one ensemble. Compare hard voting, soft voting and stacking on the same scored data.

MACHINE LEARNING · vizlearn.in/machine_learning/stacking_and_voting.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.