• Articles
  • Tutorials
  • Interview Questions

Bagging and Boosting in Machine Learning

Bagging and Boosting in Machine Learning

In this blog, we will cover what bagging and boosting are, how they are implemented, their examples, and what common points they share. So let’s get started.

Table of Content

Watch this complete course video on Machine Learning:

Video Thumbnail

What is Bagging?

Bagging, short for Bootstrap Aggregating, is a machine learning ensemble technique that involves creating multiple subsets of the original training data through random sampling with replacement. Each subset, known as a bootstrap sample, is used to train a separate model. These models, often referred to as base or weak learners, are trained independently and have no knowledge of each other.

The main idea behind bagging is to introduce diversity among the models by training them on different subsets of the data. By doing so, bagging aims to improve the overall prediction accuracy and reduce the risk of overfitting.

Get 100% Hike!

Master Most in Demand Skills Now!

How Does Bagging Work?

How Does Bagging Work?

The process of bagging provides several advantages. By training models on different subsets of the data, bagging helps reduce the impact of individual model errors and improves the overall accuracy of predictions. Here’s a step-by-step explanation of how bagging works:

Step 1: Create bootstrap samples: Starting with a training dataset of size N, bagging involves creating multiple bootstrap samples by randomly selecting N examples from the original dataset with replacement. Each bootstrap sample has the same size as the original dataset but may contain duplicate examples.

Step 2: Train independent models: For each bootstrap sample, train a separate base or weak learner model. These models are typically trained using the same learning algorithm, such as decision trees or neural networks. Each model is trained independently, without any knowledge of the other models.

Step 3: Make individual predictions: When a new example needs to be predicted, pass it through each of the trained models. Each model independently predicts the outcome based on its learned knowledge and structure.

Step 4: Aggregate predictions: Combine the individual predictions from all the models. The aggregation process depends on the problem type. For classification tasks, a common approach is to use majority voting, where the class that receives the most votes from the models is selected as the final prediction. For regression tasks, the individual predictions can be averaged to obtain the final prediction.

Step 5: Evaluate performance: Assess the performance of the bagging ensemble model by comparing its predictions against the true outcomes. Common evaluation metrics include accuracy, precision, recall, or mean squared error, depending on the problem type.

Step 6: Repeat steps 1-5 (optional): Bagging can be further improved by repeating steps 1 to 5 multiple times, creating additional bootstrap samples and training more models. This process can help to further increase the diversity among the models and improve the overall prediction accuracy.

What is Boosting?

Boosting is a machine learning ensemble technique that combines multiple weak or base learners to create a strong predictive model. It works by sequentially training models, where each subsequent model focuses on correcting the mistakes made by the previous models. The final prediction is a weighted combination of the individual models’ predictions, with more weight assigned to models that perform better.

How Does Boosting Work?

Boosting is a machine learning ensemble technique that combines multiple weak or base learners to create a strong predictive model. It works in a step-by-step fashion, with each step focusing on correcting the mistakes made by the previous learners. Here’s a step-by-step explanation of how boosting works:

Step 1: Initialize weights: Initially, all training examples are assigned equal weights.

Step 2: Train the weak learner: The first base learner is trained on the training data, considering the weights assigned to each example. The weak learner aims to minimize the error or maximize the performance metric on the training set.

Step 3: Evaluate the weak learner: The performance of the weak learner is evaluated on the training set. The examples that were misclassified or had higher errors are given higher weights, making them more important for the subsequent learners.

Step 4: Adjust weights: The weights of the misclassified examples are increased, while the weights of the correctly classified examples are decreased. This adjustment focuses on giving higher importance to the examples that the weak learner struggled to classify correctly.

Step 5: Train the next weak learner: The next weak learner is trained on the updated training data, where the weights have been adjusted. The learner focuses on the examples that were previously misclassified or had higher weights.

Step 6: Combine weak learners: The weak learners are combined to create a strong predictive model. The combination is typically done by assigning weights to the weak learners based on their individual performance.

Step 7: Steps 3 to 6 are repeated for a predetermined number of iterations or until a stopping criteria is met. Each iteration focuses on correcting the mistakes made by the previous learners and improving the overall performance of the ensemble.

Step 8: Final prediction: To make a prediction for a new example, all weak learners’ predictions are combined, typically using a weighted average or voting scheme. The weights assigned to each weak learner are based on their individual performance.

Examples of Bagging and Boosting

Both bagging and boosting are like teamwork for models. They work together to make predictions better. Bagging is like making sure everyone on the team is doing well, so it reduces mistakes and keeps things steady. Boosting is more about helping the team get smarter and learn from its mistakes to make better predictions in the future.

Bagging Examples

  1. Random Forest: Random Forest is a popular example of bagging. It combines multiple decision trees, where each decision tree is trained on a different bootstrapped subset of the training data. The final prediction is made by aggregating the predictions of all the decision trees, either through majority voting (classification) or averaging (regression).
  2. Bagging with Decision Trees: Bagging can also be applied to other models, such as decision trees. In this case, multiple decision trees are trained on different subsets of the training data, and the final prediction is obtained by averaging the predictions of all the trees.

Boosting Examples

  1. AdaBoost: AdaBoost (Adaptive Boosting) is a popular boosting algorithm. It starts by training an initial weak learner on the entire training dataset. It then iteratively focuses on the instances that were misclassified by the previous models and assigns higher weights to those instances. Subsequent models are trained to give more attention to these challenging instances, gradually improving the overall performance of the ensemble.
  2. Gradient Boosting: Gradient boosting is another widely used boosting technique. It builds an ensemble of models by sequentially training them to minimize the errors made by the previous models. Each subsequent model is trained on the residuals (the differences between the actual and predicted values) of the previous models, making it progressively better at capturing the remaining errors in the data.

Similarities Between Bagging and Boosting

Bagging and boosting are like having a group of friends to help you make better decisions, and they both focus on reducing errors to improve your decisions. Following are the similarities between bagging and boosting:

  • Ensemble Approach: Both bagging and boosting are methods where we use a group or team of models to work together.
  • Improving Predictions: They aim to make our predictions better by combining the results of multiple models.
  • Reduction of Errors: Both techniques try to reduce the mistakes that individual models might make.
  • Use of Multiple Models: In both bagging and boosting, we create several models and then bring their predictions together.

Differences Between Bagging and Boosting

Bagging and boosting are both ensemble machine learning techniques used to improve the performance of predictive models. They work by combining the predictions of multiple base models (usually decision trees) to create a more robust and accurate model. However, they differ in their approach and how they combine the base models. Here are the key differences between bagging and boosting:

BaggingBoosting
It is the easiest way to combine the predictions that belong to the same type.It is a method to combine the predictions of different types.
Each model has equal weightage.Models are weighted according to their performance.
Its target is to decrease variance, not bias.Its target is to decrease bias, not variance.
Each model is built independently.The performance of previously constructed models has an impact on newly constructed models.
It doesn’t try to correct the team’s mistakes but focuses on reducing the mistakes by working together.It can be more complex because it involves players learning from each other and becoming stronger over time.

Conclusion

In a nutshell, bagging and boosting are your dependable companions in the journey of machine learning. They represent the power of collaboration, reducing errors, and improving predictions, making complex problems more manageable and decisions more precise. Whether you opt for the stability of bagging or the progressive learning of boosting, you’ll find that these techniques are invaluable assets in your machine learning toolkit.

About the Author

Principal Data Scientist

Meet Akash, a Principal Data Scientist with expertise in advanced analytics, machine learning, and AI-driven solutions. With a master’s degree from IIT Kanpur, Aakash combines technical knowledge with industry insights to deliver impactful, scalable models for complex business challenges.