Skip to main content

Welcome to a Grasp lesson

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

Lesson 73

Ensemble Methods: Bagging and Boosting

Recommended resources

Hello! Welcome to the first lesson in our new module, "Advanced Machine Learning Models."

In the previous module, we built a strong foundation for evaluating and improving models, covering everything from the bias-variance tradeoff to regularization and hypothesis testing. You learned how to rigorously assess a single model's performance. Now, we'll shift our focus to techniques that build incredibly powerful models, often by combining simpler ones.

Today, we'll explore ensemble methods. The core idea is simple but powerful: "the wisdom of the crowd." Instead of relying on a single model, we can often achieve better performance by combining the predictions of several models. This lesson directly addresses the learning outcome: Apply ensemble methods: bagging (Random Forests) and boosting (Gradient Boosting).

We will cover:

  • The fundamental concept of ensemble learning.
  • Bagging, a technique that builds models in parallel to reduce variance, leading to the powerful Random Forest algorithm.
  • Boosting, a technique that builds models sequentially to reduce bias, with a focus on Gradient Boosting.
  • The key differences between these two powerful approaches.

1. The Core Idea of Ensemble Learning

Imagine you're trying to solve a complex problem. Instead of asking one expert, you ask a committee of experts and aggregate their opinions. The collective decision is often more robust and accurate than any single expert's opinion. Ensemble learning applies this same logic to machine learning. It combines multiple "weak learners" (models that are only slightly better than random guessing) to create one "strong learner."

Ensemble (Boosting, Bagging, and Stacking) in Machine Learning: Easy Explanation for Data Scientists

To start, let's get a clear, high-level overview of what ensemble methods are and why they are so effective. This short clip from Emma Ding provides an excellent introduction.

Watch from 00:26 to 01:27. Focus on the main principle: how a group of weak learners can form a strong learner that is more robust and less prone to overfitting.

As the video explains, by combining models, the errors of one model can be corrected by the others. This makes the final prediction more stable and reliable. The two most popular families of ensemble methods are Bagging and Boosting.


2. Bagging: Reducing Variance with Parallel Models

Bagging, which stands for Bootstrap Aggregating, is an ensemble technique primarily used to reduce the variance of a model. High variance is the hallmark of overfitting, where a model learns the training data too well, including its noise, and fails to generalize to new, unseen data. Decision trees, especially when grown deep, are prone to high variance. Bagging is an excellent way to tame them.

The process works in three steps:

  1. Bootstrap: We create multiple random subsets of the original training data. The key here is that we sample with replacement. This means some data points may appear multiple times in a single subset, while others may not appear at all. This process is called bootstrapping.
  2. Parallel Training: We train a separate model (called a base learner, often a decision tree) independently on each of these bootstrap samples. Since these models are trained on slightly different data, they will all be slightly different from one another.
  3. Aggregate: To make a final prediction, we combine the outputs of all the individual models. For a classification task, this is done by a majority vote. For a regression task, we take the average of the predictions.
Random Forest Algorithm Flowchart
This flowchart illustrates the bagging process as used in a Random Forest. Multiple bootstrap samples are created from the dataset, a decision tree is trained on each, and their predictions are combined through voting or averaging.

From Bagging to Random Forests

Random Forest is a specific and very popular implementation of bagging that uses decision trees as its base learners. It adds one extra twist to the bagging process to improve performance even further:

  • When building each tree, at each split point, it considers only a random subset of the features (columns) instead of all of them.

This additional layer of randomness helps to "decorrelate" the trees. If one feature is very predictive, standard bagging might produce many similar trees that all rely heavily on that one feature. By forcing each split to consider only a subset of features, Random Forests ensure that the ensemble includes a more diverse set of trees, leading to a more robust final model.

Machine Learning Tutorial Python - 21: Ensemble Learning - Bagging

For a deeper dive into the mechanics of bagging and Random Forests, this video from codebasics provides a clear, step-by-step explanation with helpful visualizations.

Watch from 01:39 to 07:55. The first part (until 05:46) explains Bootstrap Aggregation in detail. The second part (from 05:46) clarifies how Random Forest extends bagging by also sampling features.

Test your understanding!

Imagine you are using bagging with decision trees. Why is it generally a good idea for the individual decision trees to be "deep" and have high variance (i.e., be overfit to their bootstrap sample)?

Show answer

Bagging works by averaging out the errors of individual models. Deep, high-variance decision trees are desirable as base learners because they have low bias. They are very good at capturing the patterns in the specific subset of data they are trained on, even if it means overfitting to that subset.

When you average the predictions of many of these low-bias, high-variance trees (each overfit in a slightly different way), the individual variances tend to cancel each other out. The final aggregated model retains the low bias of the individual trees but has a much lower variance, resulting in a strong, well-generalized model.

Applying Bagging in Python

Now let's see how this works in practice. In scikit-learn, you can use the BaggingClassifier to apply the bagging technique to any base estimator, or you can use RandomForestClassifier directly.

Machine Learning Tutorial Python - 21: Ensemble Learning - Bagging

This part of the codebasics video provides a full coding tutorial on implementing a BaggingClassifier and comparing its performance to a single decision tree and a RandomForestClassifier.

Watch from 07:55 to 22:30. Follow along with the code. Pay attention to how a standalone DecisionTreeClassifier can overfit and how BaggingClassifier improves the accuracy and stability. Notice the key parameters like base_estimator and n_estimators.


3. Boosting: Reducing Bias with Sequential Models

Boosting is another powerful ensemble technique, but it works very differently from bagging. Instead of building models in parallel, boosting builds them sequentially. Each new model in the sequence attempts to correct the errors made by the previous ones.

The main goal of boosting is to reduce the model's bias. It does this by combining many "weak learners"—simple models that are only slightly better than random, like very shallow decision trees (often called "stumps").

Here's the general process:

  1. Train a simple base model on the data.
  2. Identify the instances that the model misclassified.
  3. Increase the weight of these misclassified instances, forcing the next model in the sequence to pay more attention to them.
  4. Train the next model, which will focus on getting these "hard" cases right.
  5. Repeat this process for a specified number of models.
  6. The final prediction is a weighted combination of the predictions from all models.
Comparison of Bagging and Boosting Ensemble Methods
This image clearly contrasts the parallel nature of Bagging with the sequential, error-correcting nature of Boosting.

Gradient Boosting

Gradient Boosting is a highly effective and widely used boosting algorithm. Instead of adjusting the weights of data points, it fits each new model to the residual errors of the previous model's predictions.

For a regression problem, the residual is simply (actual_value - predicted_value). The next tree is trained to predict these residuals. By adding the predictions of this new tree to the previous predictions, the overall error is reduced. This process is framed as an optimization problem where we use gradient descent to minimize the model's loss function, hence the name "Gradient Boosting."

Ensemble (Boosting, Bagging, and Stacking) in Machine Learning: Easy Explanation for Data Scientists

Let's return to the Emma Ding video for a concise explanation of how boosting works sequentially to reduce bias.

Watch from 03:40 to 05:44. Focus on the sequential nature of the process and how future learners focus on correcting the mistakes of previous learners. Note that boosting starts with high bias and works to reduce it.


4. Bagging vs. Boosting: A Summary

While both are ensemble methods, their approaches and primary goals are distinct. Understanding these differences is key to knowing when to use each.

Feature Bagging (e.g., Random Forest) Boosting (e.g., Gradient Boosting)
Training Process Parallel: Models are trained independently. Sequential: Models are trained one after another.
Primary Goal Reduce Variance (combats overfitting). Reduce Bias (combats underfitting).
Base Learners Complex, low-bias, high-variance models (e.g., deep decision trees). Simple, high-bias, low-variance models (e.g., shallow decision trees).
Model Combination Simple voting or averaging. Weighted voting/averaging, giving more influence to better models.

To solidify this comparison, let's watch the final summary from the Emma Ding video.

Ensemble (Boosting, Bagging, and Stacking) in Machine Learning: Easy Explanation for Data Scientists

This final clip neatly summarizes the core differences between bagging and boosting.

Watch from 05:44 to 06:23. This is a great recap of the key distinctions we just discussed.


Conclusion

Congratulations on completing your first lesson on advanced machine learning models! You've learned about the powerful paradigm of ensemble learning and its two main branches.

Key Takeaways:

  • Ensemble learning combines multiple models to produce a single, superior model that is more accurate and robust.
  • Bagging (Bootstrap Aggregating) trains models in parallel on different subsets of data to reduce variance and prevent overfitting. Random Forest is a prime example.
  • Boosting trains models sequentially, with each new model focusing on the errors of its predecessor, to reduce bias. Gradient Boosting is a state-of-the-art example.
  • Choosing between them depends on the problem: use bagging to control complex, high-variance models, and use boosting to build a strong model from simple, weak ones.

Preview of the Next Lesson:
In our next lesson, we will explore another highly effective and popular algorithm: Support Vector Machines (SVMs). Unlike ensemble methods, SVMs work on a completely different principle, aiming to find the optimal "decision boundary" or hyperplane that best separates data points into different classes.