Prof. Frenzel
8 min readNov 23, 2023
#KB Ensemble Learning — Part 2 — Bagging

Dear friends!

Today, I focus on Bagging, a technique that combines the insights of numerous models to increase accuracy and reduce overfitting. It’s a prominent strategy in machine learning that employs a collaborative approach for improved results. I’ll also guide you through a case study in R, showing how Bagging is practically implemented. Are you ready? Let’s go! 🚀

Theory behind Bagging

Bagging is based on the idea that combining multiple predictions can lead to more accurate and stable results than a single prediction. In bagging, each model is trained independently on a different bootstrap sample, a randomly selected subset of the data with replacement. This introduces diversity in the training data for each model, reducing the risk of overfitting and enhancing the model’s ability to generalize. While bagging enhances model stability and generalization, its effectiveness is dependent on the nature of the underlying models used in the ensemble, such as decision trees, neural networks, or support vector machines. The characteristics of these base models, whether they are prone to high variance or bias, directly influence the performance and applicability of the bagging technique in various data scenarios.

Difference from Other Models: While other ensemble methods like boosting focus on sequentially improving a model by concentrating on previously mispredicted instances, bagging runs its multiple models in parallel, each independently unaware of the others’ predictions. This parallel approach is what makes bagging unique and effective in scenarios where single models might struggle with variance in the data.

Bagging Workflow

Key Algorithms in Bagging

Random Forest is a standout in bagging algorithms. It forms a ‘forest’ of decision trees, each trained on a random data subset. The final output is determined by aggregating (typically by voting for classification, averaging for regression) the predictions from all trees. Random Forest is notable for handling various data types and its resilience against overfitting, a rare equilibrium in model training. Its randomness in selecting data points and features for each tree helps reduce variance without significantly increasing bias. The algorithm’s inherent randomness in selecting data points and features for each tree leads to a decrease in variance without a significant increase in bias, a balance that’s often hard to achieve in model training.

While Random Forest is a popular choice for bagging, it’s not always the best option. Other methods may be better suited for specific tasks or datasets. Bootstrap Aggregated Neural Networks (BANN), for instance, represent a fascinating blend of neural network versatility with the robustness of bagging. This diversity often leads to more generalized and resilient models, making BANN particularly effective in complex tasks like image and speech recognition, where data variability is high. Similarly, Bagged Support Vector Machines (SVMs) bring together the power of SVMs and the stability of bagging. SVMs are known for their effectiveness in high-dimensional spaces and for problems with a clear margin of separation. When combined with bagging, SVMs become even more potent, especially in cases where data exhibits a lot of noise or outliers. This method is particularly useful in applications like bioinformatics and text classification, where precision is key, and the data can be highly dimensional and noisy.

Pros and Cons of Bagging

Bagging has its advantages in reducing variance and improving generalization, but it also faces challenges such as the potential for maintaining bias and high computational demands.

Pros of Bagging:

  • Reduces variance: By averaging or voting the predictions of multiple models, bagging can effectively reduce the overall variance of the ensemble, leading to more stable and reliable predictions. This is particularly beneficial for unstable models, such as decision trees, that are prone to overfitting.
  • Improves generalization: Bagging models tend to generalize better to unseen data compared to individual models. This is because the ensemble is less likely to overfit to the training data and is more likely to capture the underlying patterns in the data.
  • Can handle high-dimensional data: Bagging is well-suited for problems with high-dimensional data, as it does not rely on complex feature interactions. Each model in the ensemble considers a random subset of features, making it less susceptible to the curse of dimensionality.
  • Easy to implement: Bagging is relatively simple to implement compared to other ensemble methods, such as boosting. It does not require any complex optimization procedures or sequential model training, making it computationally efficient and easier to interpret.

Cons of Bagging:

  • Bias Issues: While effective at reducing variance, bagging does not necessarily reduce bias. If all base models share the same bias, the ensemble will reflect that bias.
  • Computational Expense: Training multiple models requires significant computational resources, particularly for large datasets or complex models. This can limit bagging’s applicability in some scenarios.
  • Variable Performance Improvement: Bagging does not always lead to performance improvements across all machine learning problems. It shows its strengths in scenarios with high variance and high-dimensional data.

Based on my experience, these practices have proven to be effective strategies for optimizing the efficiency and accuracy of bagging models in various applications.

  • Efficient Computational Approaches: Parallel processing can help manage the computational demands of bagging. The use of cloud computing and optimized libraries in R and Python can be beneficial in handling these tasks. Specifically, in Python, libraries like Scikit-learn, XGBoost, Joblib, and hyperparameter tuning tools such as Hyperopt. In R, leveraging Caret, RandomForest, parallel or doParallel for parallel processing, and MLR greatly enhances the efficiency and effectiveness of bagging implementations.
  • Model Diversity: Reducing bias involves ensuring a variety of models in the ensemble. This can be done by employing different base models or altering the data sampling to introduce variation.
  • Validation Strategies: Implementing strong validation strategies is important. Techniques like k-fold cross-validation are useful for evaluating bagging models and ensuring their effectiveness on new data.
  • Hyperparameter Tuning: Careful tuning of the hyperparameters of the base models can significantly impact the ensemble’s performance. Tools such as grid search and random search can assist in this process, helping to find the optimal combination of parameters.

Case Study in R: Implementing a Random Forest Model

In this section, I’ll demonstrate how to implement a Random Forest model using R, focusing on the Boston Housing dataset, a popular choice for regression tasks. This dataset is conveniently available in the MASS package in R.

The Boston Housing dataset comprises various features, such as the average number of rooms per dwelling, property tax rate, pupil-teacher ratio, etc., with the target variable being the median value of owner-occupied homes. Its mix of continuous and categorical variables makes it ideal for demonstrating the versatility of Random Forest.

# Load necessary libraries
library(MASS)
library(caret)
library(randomForest)
library(randomForestExplainer)

# Load and prepare the dataset
data <- Boston
set.seed(123) # Setting seed for reproducibility
partition <- createDataPartition(data$medv, p = 0.8, list = FALSE)
train_data <- data[partition, ]
test_data <- data[-partition, ]

# Build and evaluate the default Random Forest model
default_rf <- randomForest(medv ~ ., data = train_data)
default_pred <- predict(default_rf, test_data)
default_rmse <- sqrt(mean((default_pred - test_data$medv)^2))

# Build and evaluate Random Forest with adjusted mtry
mtry_rf <- randomForest(medv ~ ., data = train_data, mtry = 4)
mtry_pred <- predict(mtry_rf, test_data)
mtry_rmse <- sqrt(mean((mtry_pred - test_data$medv)^2))

# Build and evaluate Random Forest with more trees (ntree)
ntree_rf <- randomForest(medv ~ ., data = train_data, ntree = 1000)
ntree_pred <- predict(ntree_rf, test_data)
ntree_rmse <- sqrt(mean((ntree_pred - test_data$medv)^2))

# Build and evaluate Random Forest with adjusted nodesize
nodesize_rf <- randomForest(medv ~ ., data = train_data, nodesize = 5)
nodesize_pred <- predict(nodesize_rf, test_data)
nodesize_rmse <- sqrt(mean((nodesize_pred - test_data$medv)^2))

# Compare RMSE data
rmse_values <- c(default_rmse, mtry_rmse, ntree_rmse, nodesize_rmse)
names(rmse_values) <- c("Default", "Mtry", "Ntree", "Nodesize")
barplot(rmse_values,
main = "Comparison of Random Forest Models",
ylab = "RMSE",
col = c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728"),
ylim = c(0, max(rmse_values) + 1))
text(x = barplot(rmse_values, plot = FALSE), y = rmse_values, label = round(rmse_values, 4), pos = 3, cex = 0.8)
Comparison of Random Forest Models

The analysis of the Root Mean Squared Error (RMSE) from each Random Forest model illuminates the influence of key tuning parameters — such as mtry, ntree, and nodesize — on the model’s accuracy. A lower RMSE typically signifies higher precision in predictions, underscoring the necessity of optimizing model complexity against performance. Adding the ‘nodesize’ parameter into the tuning process adjusts the minimum size of the terminal nodes, providing another lever to control model fit and potentially prevent overfitting. As the number of trees (ntree) increases, we might observe improved accuracy but also a rise in computational demand. Similarly, altering mtry affects feature selection during the model building process, which can have implications for how the model is interpreted. These insights are beneficial for data science practitioners, underscoring the need for careful tuning of model parameters and a comprehensive understanding of model behavior for effective use in real-world applications.

# Use randomForestExplainer to visualize output

min_depth_frame <- min_depth_distribution(default_rf)
plot_min_depth_distribution(min_depth_frame)

importance_frame <- measure_importance(default_rf)
plot_multi_way_importance(importance_frame, size_measure = "no_of_nodes")
plot_min_depth_distribution

The plot_min_depth_distrubutionoutput visualizes the distribution of minimal depths at which each variable first splits the data across all trees in the Random Forest. The colors represent the depth levels, from 0 (red) indicating a variable used at the root (first split), to 10 (pink), which are deeper, less influential splits. Variables at the top with more red (like lstat and rm) are generally more important as they consistently split data at shallower depths. Conversely, variables at the bottom (like tax and indus) split the data at greater depths, indicating a lesser importance. The numbers on the bars show the mean minimal depth, providing a quick numerical reference for comparing variable importance.

plot_multi_way_importance

The plot_multi_way_importance graph displays a scatter plot where each point represents a variable used in the Random Forest model. The x-axis ‘mean_min_depth’ indicates how early, on average, a variable is used to make a split in the trees: variables like lstat and rm, with lower mean minimal depths, are more critical as they often appear near the root of the trees. The y-axis ‘times_a_root’ shows how often a variable is used as the root split across all trees: rm and lstat stand out with higher values, suggesting they are key decision nodes. The size of the points reflects the ‘no_of_nodes’, which is the number of times a variable is used to make a split, with rm and lstat having larger points, indicating more frequent use. Collectively, these variables are important in predicting the median value of homes (medv) in the Boston area, with lstat (percentage of lower status of the population) and rm (average number of rooms per dwelling) being particularly influential.

Next up: A detailed exploration of boosting algorithms 🔗.

Prof. Frenzel
Prof. Frenzel

Written by Prof. Frenzel

Data Scientist | Engineer - Professor | Entrepreneur - Investor | Finance - World Traveler

No responses yet