Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 48

Bagging: Reducing Variance in Ensemble Models

Recommended resources

Hello! Welcome to the next lesson in our module on Tree-Based Models.

In our last lesson, we dug into the mechanics of how a single decision tree makes decisions, focusing on split criteria like Gini impurity and information gain. We concluded by noting that while individual trees are highly interpretable, they have a major drawback: they are prone to overfitting and can be very sensitive to small changes in the training data.

Today, we will directly tackle this problem. Your learning outcome is to describe how bootstrap aggregating (bagging) reduces variance in ensemble models. We'll see how the principle of "wisdom of the crowd" can be applied to machine learning models to create a final prediction that is far more stable and reliable than any single model could be on its own. This is a foundational concept for understanding modern ensemble methods, including the Random Forest model we'll cover next.

Part 1: Revisiting the Bias-Variance Tradeoff

Before we dive into bagging, let's quickly refresh our memory on a critical concept: the bias-variance tradeoff. In machine learning, the prediction error of a model can be broken down into three parts: bias, variance, and irreducible error.

Bias-Variance Tradeoff Explained
This image provides a great summary of the bias-variance tradeoff. **Bias** is error from incorrect assumptions (underfitting), while **Variance** is error from being too sensitive to the training data (overfitting). A good model balances both.

A single, fully grown decision tree, like we've discussed, is a classic example of a low-bias, high-variance model.

  • Low Bias: Because it can keep splitting until every leaf is pure, it can perfectly capture the training data, even complex patterns. It makes very few assumptions about the data's structure.
  • High Variance: This flexibility is also its weakness. It learns the training data too well, including the noise. If we were to train the same tree on a slightly different subset of our data, we might get a dramatically different tree structure. This instability is what we mean by high variance, and it leads to poor performance on new, unseen data (overfitting).

To see this idea in action, let's watch a brief clip.

Machine Learning-Bias And Variance In Depth Intuition| Overfitting Underfitting

This video from Krish Naik explains why a default decision tree is a high-variance model.

Watch the section from 12:33 to 14:03. The key takeaway is understanding that a fully grown decision tree has low bias but high variance, which means it overfits.

The core problem we need to solve is this high variance. How can we use the power of a flexible model like a decision tree without falling into the trap of overfitting? The answer lies in bagging.


Part 2: What is Bootstrap Aggregating (Bagging)?

Bagging is an acronym for Bootstrap Aggregating. It’s an ensemble technique that combines the predictions from multiple models to produce a more robust final prediction. The name itself tells you the two key steps involved.

Let's unpack these steps by reading a section from the "Hands-On Machine Learning with R" book.

Chapter 10 Bagging | Hands-On Machine Learning with R

This resource provides a clear and concise explanation of the 'why' and 'how' of bagging. It introduces the core concepts of bootstrapping and aggregation and explains when the technique is most effective.

Read the introduction and the section '10.2 Why and when bagging works'. Focus on understanding: The definition of a 'base learner'. The two main steps: creating bootstrap copies and then aggregating the predictions. The key insight: bagging works best for 'unstable, high variance' models.

To summarize the key ideas from the reading:

  1. Bootstrapping: We create many new training datasets from our original one. Each new dataset is the same size as the original and is created by sampling with replacement. This means some original data points might appear multiple times in a bootstrap sample, while others might not appear at all.

  2. Training: We train a separate model (a "base learner," like a decision tree) on each of these bootstrap datasets. Because each dataset is slightly different, we end up with a collection of slightly different models.

  3. Aggregating: To make a new prediction, we get a prediction from every model in our ensemble and combine them.

    • For regression, we average the predictions.
    • For classification, we take a majority vote (or average the predicted probabilities).

This process is visualized beautifully in the following diagram.

Bagging Process Illustration with Key Questions
This diagram illustrates the bagging workflow. A single training set is used to generate multiple bootstrapped datasets. An independent model (in this case, a decision tree) is trained on each one. Their collective output forms the final prediction.

Part 3: How Averaging Reduces Variance

So, we've trained many high-variance models on slightly different data. How does averaging their results lead to a low-variance ensemble?

The core intuition is that while each individual model overfits the data in its own way (capturing different bits of noise), these errors are largely random. When we average the predictions, these random errors tend to cancel each other out, leaving behind the true underlying signal.

There's a simple statistical principle behind this. The variance of the average of independent random variables is times the variance of a single one of those variables.

In our case, the are our individual tree models. By averaging trees, we could theoretically reduce the variance by a factor of .

The Important Caveat: In bagging, our models are not perfectly independent. They are all trained on bootstrap samples taken from the same original dataset. This means the trees will be correlated. If there's a strong predictor in the dataset, most trees will likely use it for their first split. This correlation limits the amount of variance reduction we can get, but the reduction is still very significant.

Let's watch a video that explains this key concept.

Introduction to Machine Learning - 08 - Boosting, bagging, and random forests

This video from Tübingen Machine Learning provides a fantastic explanation of how bagging works to reduce variance while maintaining low bias.

Watch from 21:17 to 25:53. Pay close attention to: The definition of a 'bootstrap dataset'. The main idea: apply bagging to low-bias, high-variance models. The explanation that averaging keeps the bias low but causes the variance to go down. The mention that the variance doesn't go to zero because the models are not truly independent.

So, to summarize the magic of bagging:

  • We start with a high-variance, low-bias learner (like a deep decision tree).
  • We create many versions of it by training on bootstrap samples.
  • The bias of the ensemble remains low (averaging many unbiased estimates gives an unbiased estimate).
  • The variance of the ensemble is significantly reduced because the individual models' errors, driven by noise in their respective training samples, tend to cancel out upon averaging.
Test your understanding!

The reading mentioned that for "algorithms that are more stable or have high bias, bagging offers less improvement."

Based on what you've learned, why would bagging not be very effective if you used a simple linear regression model as the base learner?

Show answer

A simple linear regression model is a low-variance, high-bias model.

  • Low Variance: It's stable. If you train a linear model on slightly different subsets of the data, the resulting regression line won't change very much.
  • High Bias: It makes a strong assumption that the relationship between features and the target is linear. If the true relationship is more complex, the model will systematically fail to capture it.

Since bagging's primary benefit is variance reduction, applying it to a model that already has low variance yields little improvement. Averaging many very similar regression lines will just give you back the original regression line. Bagging does not reduce bias, so the fundamental limitations of the linear model would remain.


Conclusion

You've now uncovered the statistical reasoning behind one of the most powerful ensemble techniques. You can now explain not just what bagging is, but why it is so effective at improving the performance of models like decision trees.

Key Takeaways:

  • Single decision trees are often low-bias, high-variance models, which means they are prone to overfitting.
  • Bagging (Bootstrap Aggregating) is an ensemble method that combats this by training many trees on different bootstrap samples (sampling with replacement) and aggregating their predictions.
  • The core benefit of bagging is variance reduction. By averaging the predictions of many models, the random errors and noise captured by each individual model tend to cancel out.
  • Bagging is most effective when applied to unstable, high-variance models. The bias of the ensemble remains roughly the same as the bias of the base learners.

Preview of the Next Lesson:
Bagging is a huge step forward, but we can do even better. The main limitation of bagging is that the trees can still be quite correlated, which limits the total variance reduction. In our next lesson, we will explore Random Forests, a clever modification of bagging that introduces an extra element of randomness to deliberately de-correlate the trees, leading to an even more powerful and robust model.