Prof. Frenzel
16 min readNov 25, 2023
#KB Ensemble Learning — Part 3 — Boosting

Dear friends!

Imagine if we could train machines to learn from their errors and improve over time. Today, we focus on Boosting, a machine learning approach that incrementally improves predictive accuracy. We’ll delve into the principles of Boosting, its various forms like XGBoost and AdaBoost, and discuss the challenges they help overcome. I’ll also provide a brief case study using the Boston Housing dataset, showing how Boosting is practically implemented. Are you ready? Let’s go! 🚀

What is Boosting?

When we talk about boosting in the context of machine learning, we’re referring to a family of algorithms that have become integral in the field for their ability to improve the accuracy of predictions. Boosting iteratively trains models, focusing on the errors of previous models to refine and improve predictive power. With each iteration, the model learns from past mistakes, resulting in a strong ensemble model despite starting with individually weak models.

Having spent over 15 years in this domain, I can attest to the significant industry impact following the introduction of XGBoost in 2014, a highly efficient and scalable implementation of gradient boosting. Its contribution to enhancing prediction accuracy in machine learning is unparalleled. Where traditional algorithms falter with complex, nuanced datasets, boosting excels, refining a model’s comprehension and data interpretation. Boosting elevates model development from simple error fixing to more advanced comprehension and manipulation of data. In finance, boosting algorithms have become integral in developing Time-Series Classification strategies and ensuring robust risk management. They excel at identifying complex patterns in financial data, essential for making accurate predictions and decisions. This is particularly important in fast-paced markets where patterns can change rapidly. Boosting algorithms in healthcare have become the standard in improving disease diagnosis. They analyze extensive symptom data and patient histories, leading to more precise diagnostic outcomes and assisting doctors in making more precise diagnoses. This approach is effective for managing large and complex datasets, where each piece of information can be important.

If you review Kaggle over the past few years, XGBoost has become the best friend of many winning teams!

Principles of Boosting

Understanding the principles of boosting requires us to look at how it incrementally improves a model. In the case of boosting, each iteration pays more attention to the instances that previous models misclassified. This is done by assigning more weight to the errors, so the next model in the sequence focuses more on these challenging cases. This iterative error correction continues until the model achieves the desired level of accuracy or no further improvements can be made.

Boosting Workflow

One aspect that I find particularly compelling about boosting is its ability to adaptively fine-tune the model. Unlike other machine learning techniques, boosting doesn’t just passively aggregate predictions; it actively evolves, learning from the mistakes of previous iterations. This characteristic not only improves accuracy but also offers insights into the data’s underlying complexities and the changing conditions in real-world applications, often revealing hidden patterns that simpler models might miss.

Types of Boosting Algorithms

1️⃣AdaBoost — The Pioneer in Ensemble Learning

AdaBoost, a practical application of the boosting principles, transforms basic weak learners into a strong, cohesive model. These weak learners are typically decision trees with a single decision node and two outcomes. Unlike in random forests where each tree has an equal vote in classification, AdaBoost assigns differing weights to each weak learner, giving more influence to some over others. The construction of each model in AdaBoost is sequential, with each model being influenced by the errors of its predecessor. This sequential process allows AdaBoost to focus more on the instances that previous models classified incorrectly, thus continuously improving its predictive performance. AdaBoost is widely used with decision trees, but it can be adapted to work with other types of models as well.

2️⃣Gradient Boosting — The Downhill Racer

Gradient Descent is a method used in machine learning to optimize the loss function, a measure of how far off a model’s predictions are from the actual results. It involves iteratively moving the model’s parameters down the slope of the cost function to find its minimum point, where the predictions are most accurate. A small learning rate means the model makes tiny, careful steps down this slope, reducing the risk of overshooting the minimum, but possibly taking longer to get there. Conversely, a large learning rate results in bigger steps, which can speed up the process but also risk missing the lowest point if the steps are too large.

Gradient Descent

Gradient Boosting builds upon the principles of Gradient Descent. This process involves using the Gradient Descent methodology to minimize the loss function, but it does so across multiple models, each correcting the errors of the last. Each new model in the sequence focuses on the residual errors (differences between the predicted and actual outcomes) of the previous model. This process involves calculating the gradient of the loss function, hence the name. Like walking down a hill in the smallest possible steps, each model takes a step in the direction that reduces the loss most significantly. This stepwise refinement continues until the addition of new models no longer significantly decreases the loss, or a set number of models have been added. The result is a highly accurate predictive model, adept at handling a wide range of complex data sets.

3️⃣XGBoost — Welcome to Formula One 🏎️

Gradient Boosting aims to minimize the overall error of the model, but XGBoost, or Extreme Gradient Boosting, goes a step further. Just as engineering excellence is key in Formula One, XGBoost has revolutionized the standard Gradient Boosting Method (GBM) through meticulous systems optimization and algorithmic enhancements. This optimization mirrors the precision and efficiency seen in Formula One engineering, where every component is fine-tuned for peak performance. Similarly, XGBoost’s advanced features, such as handling missing data and providing parallel processing capabilities, represent the emphasis on efficiency and speed in machine learning.

System and Algorithmic Optimization in XGBoost

System and Algorithmic Optimization in XGBoost:

  1. Parallelization: One of the most impactful system optimizations in XGBoost is parallelization. Traditional GBM builds trees sequentially, which can be time-consuming. XGBoost, however, parallelizes this process, meaning it builds multiple trees at the same time, significantly speeding up the learning process. This is achieved by reordering the computational loops in the algorithm, allowing for simultaneous calculations.
  2. Tree Pruning: In the GBM framework, trees are built greedily, which means they keep growing until a stopping criterion is met. XGBoost introduces an improvement with a ‘depth-first’ approach. It uses a ‘max_depth’ parameter and starts pruning trees backward from the maximum depth, improving computational efficiency.
  3. Hardware Optimization: XGBoost is designed to utilize hardware resources more efficiently. It includes cache-aware operations and ‘out-of-core’ computing, which helps in handling large datasets that do not fit into memory. This optimization ensures better performance, especially when dealing with big data.
  4. Regularization: Regularization is a technique used to prevent overfitting, where the model performs well on training data but poorly on unseen data. XGBoost includes both L1 (LASSO) and L2 (Ridge) regularization. These regularization terms add penalties for more complex models, helping to keep them simpler and more generalizable.
  5. Sparsity Awareness: ‘Sparse data’ refers to datasets with a lot of missing or zero-valued elements. XGBoost is efficient in handling sparse data. It can automatically identify and learn the best way to handle missing values, improving performance on datasets with lots of missing data.
  6. Cross-validation: XGBoost has a built-in Cross-validation method, which eliminates the need for manual implementation. This feature helps determine the number of boosting iterations required, minimizing the risks of underfitting and overfitting.

In my experience, XGBoost has consistently demonstrated superior performance and efficiency compared to other machine learning algorithms. Its use of regularization and tree-based models has contributed significantly to its robustness and transparency. Unlike other complex, black-box models, such as neural networks, XGBoost’s decision tree framework makes it more interpretable and easier to understand. This transparency, coupled with its resistance to overfitting, makes XGBoost a reliable choice in various structured data scenarios. While no single algorithm is universally optimal, XGBoost’s remarkable performance and versatility make it a powerful tool for data scientists and machine learning practitioners alike.

4️⃣LightGBM: The Lightweight

LightGBM, or Light Gradient Boosting Machine, is a sophisticated variation of the gradient boosting framework, designed to be more efficient and faster, especially with large datasets. It stands out due to its implementation of a unique approach called Gradient-based One-Side Sampling (GOSS) and Exclusive Feature Bundling (EFB). GOSS allows LightGBM to focus more on the data points with larger gradients, i.e., those that are more difficult to predict, effectively enhancing the learning from these points. EFB reduces the dimensionality of the data by bundling exclusive features, thereby speeding up the computation without sacrificing accuracy.

This algorithm also employs a new way of building trees called leaf-wise growth, as opposed to the more traditional level-wise growth used in other gradient boosting models. Leaf-wise growth allows the model to converge faster and often with greater accuracy, though it can be more prone to overfitting with small datasets. LightGBM’s efficient handling of large datasets and higher speed with comparable accuracy makes it a favored choice in machine learning competitions and practical applications where performance and speed are essential.

Pros and Cons of Boosting

Boosting algorithms offer significant advantages in reducing bias and improving accuracy, but they can also be computationally expensive and may require careful hyperparameter tuning to avoid overfitting.

Pros of Boosting:

  • Improves Accuracy: Boosting algorithms are known for their ability to create highly accurate predictive models. By focusing on correcting the mistakes of previous iterations, they are particularly effective in complex problems where simple models fail.
  • Reduces Bias: Unlike bagging, boosting primarily focuses on reducing bias. By sequentially adding models that address the weaknesses of the previous ones, boosting creates an ensemble that is often more accurate than individual models, especially in cases where underfitting is a concern.
  • Handles Weak Learners Efficiently: Boosting is designed to transform weak learners into strong ones. This makes it particularly useful when dealing with models that perform only slightly better than random guessing, yet can be significantly improved through ensemble techniques.
  • Flexibility: These algorithms are versatile in dealing with various data types, be it numerical, categorical, or textual. This adaptability makes them suitable for a wide range of applications, from image recognition to financial modeling.

Cons of Boosting:

  • Prone to Overfitting: If not carefully monitored, boosting can overfit, especially in the presence of noise in data. Its focus on correcting misclassifications can lead to a model overly complex and tailored to the training data.
  • Computationally Intensive: Sequential model training in boosting is more time-consuming and computationally expensive compared to parallel methods like bagging. This can be a limiting factor when dealing with large datasets or requiring quick model deployment.
  • Sensitivity to Outliers: Boosting algorithms can be overly influenced by outliers. Since they focus on correcting misclassifications, outliers can lead to skewed adjustments, thereby affecting the model’s overall accuracy.
  • Complex Parameter Tuning: Achieving the optimal performance with boosting requires careful tuning of parameters, which can be a complex and time-consuming process. This complexity might pose a challenge, especially for those new to machine learning.
  • Less Interpretability: As boosting models are often complex and involve numerous weak learners, they can lack the interpretability of simpler models. This can be a disadvantage in fields where understanding the decision-making process is as important as the accuracy of the predictions.

Overcoming the challenges posed by boosting requires strategic approaches and best practices, which I’ve found to be effective in various projects.

  • Preventing Overfitting: To combat the risk of overfitting in boosting models, one effective strategy is to use early stopping. This involves monitoring the model’s performance on a validation set and stopping the training process once the performance starts to degrade. Regularization techniques, like L1 or L2 regularization, can also be applied to penalize complex models and prevent them from fitting the noise in the data.
  • Managing Computational Resources: To address the high computational demands of boosting algorithms, techniques such as parallel processing and optimizing the efficiency of weak learners can be employed. Additionally, using algorithms designed for efficiency, like LightGBM, can help manage large datasets more effectively.
  • Parameter Tuning: To address the sensitivity of boosting algorithms to parameter settings, I recommend a systematic approach to parameter tuning. Techniques like grid search, random search, or automated machine learning (AutoML) tools can help in efficiently finding the optimal hyperparameters.
  • Enhancing Interpretability: Boosting models, especially those based on decision trees, can become complex and difficult to interpret. To improve interpretability, one can limit the depth of the trees or the number of trees in the ensemble. Visualization tools, like feature importance plots, can also be used to understand the contribution of different features to the model’s predictions.
  • Handling Noisy Data: In datasets with a lot of noise, boosting algorithms can overfit to the noise rather than the signal. To mitigate this, one practice is to clean the data by removing outliers or applying smoothing techniques. Additionally, incorporating noise-robust loss functions in the boosting algorithm can help in focusing the model on the underlying patterns rather than the noise.

Optimization and Parameter Tuning

Optimization and parameter tuning are critical in maximizing the performance of boosting algorithms. This process involves adjusting various settings and configurations to find the most effective combination for a specific dataset. Based on my experience, here are some best practices:

  • Learning Rate Adjustment: The learning rate controls how quickly or slowly a boosting model adapts to the problem. A lower learning rate means the model learns slowly, reducing the risk of overfitting but increasing training time. Conversely, a higher learning rate speeds up training but can lead to overfitting. Balancing the learning rate is key to achieving optimal performance.
  • Number of Learners: Determining the right number of weak learners (trees in the case of tree-based models) can be time consuming. Too few might lead to underfitting, while too many can cause overfitting. Cross-validation can help in identifying the ideal number of learners for a given problem.
  • Tree-Specific Parameters: For tree-based boosting models, parameters like depth of the tree, minimum number of samples required to split a node, and minimum number of samples required at a leaf node are important. These parameters control the complexity of the trees and, consequently, of the overall model.
  • Handling Overfitting with Regularization: Regularization techniques, like L1 and L2 regularization, add a penalty to the loss function to control the model’s complexity. This helps in preventing overfitting by discouraging overly complex models.
  • Data and Feature Subsampling: Employing subsampling for training data and features in creating each weak learner can enhance the model. This approach, known as Stochastic Gradient Boosting, introduces randomness, improving robustness.
  • Automated Hyperparameter Tuning: Tools like Grid Search, Random Search, or Bayesian Optimization can automate the process of finding the best hyperparameters. These methods systematically explore a range of values and can significantly streamline the tuning process.

Each dataset has its own quirks, and there’s no one-size-fits-all set of parameters. Iterative experimentation and validation are key. In Python, scikit-learn’s GridSearchCV and RandomizedSearchCV automate parameter tuning, streamlining the process. For R users, packages like caret and mlr offer similar functionality, providing robust frameworks for systematic experimentation and model optimization. These tools facilitate a more systematic and efficient approach to model optimization, tailoring the process to the specific needs of each dataset.

Case Study in R: Implementing XGBoost

Drawing from our previous experience with the Boston Housing dataset in the bagging article, we will revisit this classic dataset to investigate XGBoost.

The main focus is on experimenting with various hyperparameters like max_depth, eta, gamma, and regularization terms (lambda and alpha). For each combination of these parameters, the code trains an XGBoost model and evaluates each model on the test data using RMSE (Root Mean Squared Error) as the performance metric. This approach is great for understanding how different settings affect the model’s performance, helping you determine the best hyperparameters for your specific task.

# Load necessary libraries
library(MASS)
library(caret)
library(xgboost)
library(dplyr)

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

# Data Preprocessing
train_x <- data.matrix(train_data[, -14])
train_y <- train_data$medv
test_x <- data.matrix(test_data[, -14])
test_y <- test_data$medv

# Define a grid of hyperparameters to test
hyper_grid <- expand.grid(
max_depth = c(3, 6, 9),
eta = c(0.05, 0.1, 0.3),
gamma = c(0, 1, 5),
lambda = c(1, 1.5, 2),
alpha = c(0, 0.5, 1)
)

# Initialize a data frame to store the results
results_df <- data.frame(max_depth = integer(), eta = double(), gamma = double(),
lambda = double(), alpha = double(), RMSE = double())

# Model Building and Evaluation
for(i in 1:nrow(hyper_grid)) {
params <- list(
booster = "gbtree",
objective = "reg:squarederror",
eta = hyper_grid$eta[i],
max_depth = hyper_grid$max_depth[i],
gamma = hyper_grid$gamma[i],
lambda = hyper_grid$lambda[i],
alpha = hyper_grid$alpha[i]
)

xgb_train <- xgboost(
params = params,
data = train_x,
label = train_y,
nrounds = 100,
verbose = 0
)

# Predict and evaluate
predictions <- predict(xgb_train, test_x)
rmse <- sqrt(mean((predictions - test_y) ^ 2))

# Store the results
results_df[i, ] <- c(hyper_grid$max_depth[i], hyper_grid$eta[i],
hyper_grid$gamma[i], hyper_grid$lambda[i],
hyper_grid$alpha[i], rmse)
}

# Rename the columns appropriately
colnames(results_df) <- c("max_depth", "eta", "gamma", "lambda", "alpha", "RMSE")

The histogram below illustrates the distribution of RMSE values derived from various hyperparameter combinations we tested. The RMSE shows a cluster around 3.0, suggesting most model configurations yield similar accuracy.

RMSE Distribution of Parameter Tuning

The scatter plots visualize the relationship between our performance measure, RMSE, and five XGBoost hyperparameters: max_depth, eta, gamma, lambda, and alpha. There isn’t a distinct pattern showing the impact of max_depth and eta on RMSE, implying potential insensitivity within the tested ranges. Similarly, gamma, which is associated with regularization to avoid overfitting, doesn’t display a strong connection to RMSE. The lambda and alpha parameters, representing L2 and L1 regularization respectively, also do not exhibit a clear influence on model performance, suggesting the current hyperparameter values might not significantly affect the model’s predictive accuracy.

RMSE vs Parameters we tested

Model interpretation

The RMSE dataframe (see results_df) gives us an idea of how sensitive our XGBoost model is to the parameters we tested. To assist us further in the interpretation of our XGBoost model, we can use xgb.importance (similar to our approach in the random forest article) and SHAP (SHapley Additive exPlanations). SHAP values are derived from game theory and offer a granular view of how each feature influences individual predictions. Imagine each feature as a player in a soccer game where the prediction is the score count. SHAP values tell us how to fairly distribute the score count among the players on the field. They do this by considering all possible combinations of features, determining the contribution of a feature in each combination, and then averaging these contributions. This detailed breakdown allows us to see which features are most influential and how they interact to arrive at the final prediction, offering a deeper insight into model behavior beyond just overall feature importance.

# Feature importance assessment with XGBoost
importance_matrix <- xgb.importance(feature_names = colnames(train_x), model = xgb_train)
xgb.plot.importance(importance_matrix)

# Load SHAPforxgboost for model interpretability
library(SHAPforxgboost)

# Identify the model with the lowest RMSE (example! be careful with overfitting!)
best_row <- results_df[which.min(results_df$RMSE), ]

# Retrieve optimal hyperparameters from the best model
best_params <- list(
booster = "gbtree",
objective = "reg:squarederror",
eta = best_row$eta,
max_depth = best_row$max_depth,
gamma = best_row$gamma,
lambda = best_row$lambda,
alpha = best_row$alpha
)

# Re-Build the best model using the optimal hyperparameters
best_model <- xgboost(
params = best_params,
data = train_x,
label = train_y,
nrounds = 100,
verbose = 0
)

# Generate and plot SHAP values for model explanation
shap_values <- shap.values(xgb_model = best_model, X_train = train_x)
shap_long <- shap.prep(xgb_model = best_model, X_train = train_x)
shap.plot.summary(shap_long)

# Visualize the impact of top features on model's predictions
plot1 <- shap.plot.dependence(data_long = shap_long, x = "lstat") + ggtitle("SHAP for 'lstat'")
plot2 <- shap.plot.dependence(data_long = shap_long, x = "rm") + ggtitle("SHAP for 'rm'")
plot3 <- shap.plot.dependence(data_long = shap_long, x = "nox") + ggtitle("SHAP for 'nox'")
Feature Importance graph from the XGBoost

The feature importance plot generated by the XGBoost model provides a straightforward indication of which variables from the Boston housing dataset have the most significant impact on predicting house values. The lstat feature, indicating lower socioeconomic status, emerges as the top influencer, suggesting a strong inverse relationship with house value—areas with higher lstat values may see lower house prices. Following this, the number of rooms (rm) also shows a substantial effect, with a direct correlation where more rooms typically predict higher house values. Other features like dis (distances to employment centers) and crim (crime rate) are also informative but to a lesser extent. Interestingly, the chas variable, denoting proximity to the Charles River, appears to have minimal influence on the model's predictions.

SHAP summary plot

In the SHAP summary plot above, each point represents a SHAP value for a feature and an individual prediction. The position on the horizontal axis indicates the impact on the model output. Points to the right indicate an increase in the prediction value, while points to the left indicate a decrease. The color represents the feature value (from low to high). Features like lstat show a significant spread of SHAP values, primarily shifting predictions to lower values, suggesting that higher lstat figures correlate with decreases in house prices. Conversely, rm appears to mostly push predictions upward, implying that a greater number of rooms tends to increase house prices. Other features such as crim and dis display a mix of positive and negative effects on the housing value predictions, indicating their varied influence depending on the specific context within the data. The minimal spread of points for chas near the zero line suggests its limited role in affecting the model's predictions.

SHAP dependence plots for lstat, rm, and nox

The SHAP dependence plots offer insights into how certain features from the Boston housing dataset affect house value predictions made by an XGBoost model. For lstat, the trend is clear: as the value rises, it typically leads to a decrease in predicted house values, indicating a reliable influence of this socioeconomic factor. The rm plot presents a positive correlation, with the number of rooms showing a strong relationship to increasing house values, although the effect varies more with higher room counts. The nox plot, representing nitrogen oxides concentration, displays a non-linear relationship with house values, initially showing a slight increase in predicted value with higher nox levels, then a decrease, suggesting that while nox does affect house prices, the relationship is complex and potentially influenced by the interaction with other variables.

Prof. Frenzel
Prof. Frenzel

Written by Prof. Frenzel

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

No responses yet