Machine Learning Algorithms

Ch.41: Ensemble Learning, the Introduction

By Ayush Arora••10 min read

Inspired by: YouTube

This is the start of a big, practical block of the series: ensemble learning. The idea shows up in almost every real machine learning project and almost every Kaggle solution, because it is usually the cheapest way to squeeze out a few more points of accuracy after everything else is done. This post is only the introduction: what the word means, why it works, and the four shapes it comes in. Each shape gets its own post afterwards.

There is no companion notebook for this video; the visuals below are regenerated from scratch with scikit-learn to make the same points the video makes with stock illustrations.


1. What "Ensemble" Means

Google "ensemble" and you get "a group of things viewed as a whole." In machine learning it means exactly that: instead of training one model, you train a collection of models and combine their outputs into one bigger model. That combined thing is the ensemble.

The base models can be anything: a linear regression, a decision tree, an SVM, a KNN, or ten thousand copies of the same algorithm. What matters is what you do with their answers, and (as Section 3 explains) how different they are from each other.

2. Wisdom of the Crowd

Before the mechanics, the belief the whole field rests on: a large, diverse crowd is often collectively smarter than any single expert in it. This is not an ML idea, it is everywhere:

There's an old anecdote, a couple hundred years old by some accounts, of a fair where a host put an animal on stage and asked the crowd to guess its weight, prize to whoever got closest. No single guess was exact, but the average of everyone's guesses landed remarkably close to the true weight. Somehow the crowd, collectively, knew, and The Wisdom of Crowds by James Surowiecki is the usual pointer for reading more on why.

Ensemble learning is this fact turned into an algorithm: gather many models, ask each one, and decide from their combined answer.

3. The Prediction Step

Training differs a lot between ensemble types and is deferred to the later posts. The prediction step, though, is the same everywhere and is worth pinning down now.

Say you have a trained ensemble of five base models. A new data point arrives, for example a student with a known CGPA and IQ, and the question is whether they get placed.

Classification. Hand the point to all five models. Suppose three say "placed" and two say "not placed." The ensemble takes the majority vote: "placed." Whatever class most base models predict is the ensemble's prediction.

Regression. Same setup, but now each model outputs a number: 8.1, 3.2, 7.5, 6.9, 7.1 lakh. The ensemble takes the mean. Nothing else changes.

4. The One Rule: Base Models Must Differ

An ensemble of five identical models is just one model asked five times. For the crowd to be wise, its members have to bring different information.

The KBC analogy again: if every person in the audience is a software developer, "ask the audience" only helps on computer science questions and is useless on everything else. An audience with a chartered accountant, a civil engineer, a doctor, a politician, and a philosopher covers far more ground; whatever the question, someone probably knows.

There are two ways to manufacture that difference between base models:

  1. Different algorithms, same data. Model 1 is linear regression, model 2 is an SVM, model 3 is a decision tree, all trained on the same dataset. They disagree because they see the data through different assumptions.
  2. Same algorithm, different data. All models are, say, decision trees, but each is trained on a different subset of the rows. They disagree because they learned from different examples.

A third option is to combine both. These two levers are exactly what separate the four ensemble families below.

5. The Four Families

FamilyHow base models differHow outputs combine
Votingdifferent algorithms, same dataplain majority vote / mean
Stackingdifferent algorithms, same dataa second model learns how much to trust each base model
Baggingsame algorithm, different random subsets of dataplain majority vote / mean
Boostingsame algorithm, trained in sequenceeach model fixes the previous model's mistakes

Voting

The simplest. Pick a few different algorithms, say KNN, logistic regression, and a decision tree. Train all of them on the same dataset. To predict, pass the new point to all three and take the majority vote (classification) or the mean (regression). All base models count equally. The diversity comes entirely from using different algorithms.

Stacking

Voting with one change: instead of every model getting an equal vote, a separate machine learning model sits on top and learns weights for the base models, based on which ones tend to be right. Analogy: normal democracy gives everyone one vote; stacking is a democracy that first works out whose judgement to weight more heavily. The meta-model is trained on the base models' predictions (plus the true labels) and outputs the final answer.

Bagging

Here the base models are all the same algorithm. The difference comes from the data. Given 1,000 training rows and a target of 500 rows per model, you draw 500 rows at random with replacement (a "bootstrap sample"), train a model on them, put the rows back, and draw another 500 for the next model. Each model ends up trained on a different slice, so each learns something slightly different. Predict by majority vote / mean, same as voting. The name is short for bootstrap aggregating.

Random Forest is the special case of bagging where every base model is a decision tree. A forest is a collection of trees; that is where the name comes from. It is one of the most useful algorithms in practice and gets its own posts.

Boosting

Boosting also uses the same algorithm repeatedly, but in sequence rather than in parallel. The first model trains on the data and notes which rows it got wrong. Those mistakes are passed to the second model, which is told to pay extra attention to them; it improves on those rows but makes its own errors, which pass to the third model, and so on. Each model in the chain is weak on its own, but the running combination keeps shrinking the overall error. The famous names here are AdaBoost, Gradient Boosting, and XGBoost, the last of which dominates tabular Kaggle competitions.

6. Why Aggregation Works

Two pictures give the intuition.

Classification. Train several unpruned decision trees, each on its own bootstrap sample. Individually their decision boundaries are jagged and they disagree with each other, each one has memorised the quirks of its particular sample. Take a majority vote across many of them and the noise in any single boundary gets outvoted, leaving a cleaner boundary than any one tree drew.

Four panels on a two-moons dataset. The first three show the jagged, blocky decision boundaries of three separate decision trees, each trained on its own bootstrap sample, and each looks noticeably different. The fourth panel shows the majority vote of 101 such trees, whose boundary is visibly smoother and follows the curve between the two classes more cleanly than any individual tree

Regression. Train many unpruned decision-tree regressors, each on its own noisy sample of the same underlying curve. Each one is a wild staircase that chases the noise. Average them and the individual overshoots and undershoots cancel, and the mean curve tracks the true function closely.

A regression plot over a sine-shaped true function shown as a black dashed line. Dozens of faint orange step functions, the individual decision trees, swing wildly above and below the curve. A thick blue line, the average of 300 trees, sits almost exactly on top of the true dashed curve

In both cases the ensemble is not doing anything clever per model; it is letting uncorrelated errors cancel.

7. Benefits

8. The Cost

There is one real disadvantage: compute. Where you used to train one model, you now train many, so training is slower and heavier. Ensembles are worth it because the accuracy gain usually pays for the extra cost, but the trade is real.

9. When to Use It

Treat an ensemble as the last step. Do the data cleaning, the preprocessing, the feature engineering, build and tune a model, evaluate it, and then wrap an ensemble around it. Most of the time the result improves.

If you work as a data scientist, use ensembles on real projects. In Kaggle-style competitions they are effectively mandatory; top entries lean on them heavily, and XGBoost in particular became famous largely because of how well it did in Kaggle competitions.

One more practical note from the video: on small to medium tabular datasets, well-built ensemble methods very often beat deep learning, so this block of the series is worth learning well before reaching for a neural network.

10. What's Next

The rest of this block works through the families one at a time: voting first, then bagging and random forest, then the boosting algorithms, and finally stacking.