A Complete Guide to Boosting Methods for Better Predictions (with R Code)
Introduction to Boosting
Boosting is an ensemble machine learning technique that combines multiple weak learning models to produce a strong learner. The idea is to train models sequentially, where each subsequent model attempts to correct the errors of the previous model. In this way, boosting can significantly improve the accuracy and robustness of predictions compared to using a single model.
Boosting is often contrasted with bagging (bootstrap aggregating), another popular ensemble method. While bagging trains models independently in parallel on bootstrap samples of the data, boosting trains models additively in sequence. The boosting approach allows models to specialize and prioritize harder to classify examples.
Key Concepts in Boosting
The boosting process follows these high-level steps:
- Start with a weak learner like a decision tree
- Fit the learner to the training data
- Increase the weights of misclassified examples and decrease weights of correctly classified ones
- Train a new weak learner on the reweighted data
- Combine the weak learners into a strong classifier, weighting each based on its performance
- Repeat steps 3-5 for a number of iterations or until a stopping criteria is met
Mathematically, the final boosted model prediction F(x) is the weighted sum of M weak learners:
F(x) = ∑ᵢ₌₁ᴹ αᵢ · hᵢ(x)
where hᵢ(x) is the prediction of the i-th weak learner and αᵢ is its weight, calculated based on its error rate εᵢ:
αᵢ = ln((1-εᵢ) / εᵢ) / 2
Intuitively, models that perform better (lower error rate) receive higher weights in the ensemble. The weights of misclassified examples also get increased in each iteration such that subsequent models focus more on the hard examples.
Gradient Boosting Machines
Gradient Boosting Machine (GBM), also known as Gradient Boosted Regression Trees (GBRT), is one of the most popular and effective boosting algorithms. GBM builds an additive model in a forward stage-wise fashion, implementing boosting by optimizing an arbitrary differentiable loss function.
The high-level steps of the gradient boosting algorithm are:
- Initialize the model with a constant value
- For m = 1 to M:
a) Compute so-called pseudo-residuals
b) Fit a weak learner (e.g. regression tree) hᵐ(x) to pseudo-residuals
c) Compute multiplier γₘ by solving optimization problem
d) Update model: Fₘ(x) = Fₘ₋₁(x) + γₘ·hₘ(x) - Return final model F(x) = Fₘ(x)
The pseudo-residuals are the gradients of the loss function, which tell the model which direction to improve. For each iteration, a weak learner is fit to these residuals. Then the model is updated to minimize the loss, controlled by the learning rate γₘ. The final model is the sum of the weak learners from each stage.
GBM in R
Let‘s see how to implement GBM in R using a step-by-step example on a real dataset. We‘ll use the popular gbm package.
First, install and load the required packages:
install.packages(c("gbm", "caret", "mlbench"))
library(gbm)
library(caret)
library(mlbench)
Next, load the data. We‘ll use the Sonar dataset which has 60 predictors and a binary response of "Rock" or "Mine". Split the data into train and test sets.
data(Sonar)
set.seed(123)
inTraining <- createDataPartition(Sonar$Class, p = .75, list = FALSE)
training <- Sonar[ inTraining,]
testing <- Sonar[-inTraining,]
Now let‘s train the GBM model. The key parameters are the number of trees (n.trees), depth of trees (interaction.depth), learning rate (shrinkage), and subsampling rate (bag.fraction). We‘ll use 10-fold cross-validation to find the optimal number of trees.
gbmGrid <- expand.grid(interaction.depth = c(1, 3),
n.trees = c(50, 100, 150),
shrinkage = 0.1,
n.minobsinnode = 10)
gbmFit <- train(Class ~ ., data = training,
method = "gbm",
trControl = trainControl(method = "cv", number = 10),
tuneGrid = gbmGrid,
verbose = FALSE)
gbmFit
This outputs the cross-validated accuracy for each tuning parameter. To see which predictors the model finds most important, use the summary function:
summary(gbmFit$finalModel, las = 2, cBars = 10, cex.names = 0.6)
Finally, we can make predictions on the test set and evaluate performance:
gbmPred <- predict(gbmFit, newdata = testing)
confusionMatrix(gbmPred, testing$Class)
The confusion matrix shows that GBM achieves a 83% accuracy on the test set, which is quite good. We could further tune the other hyperparameters like learning rate and subsampling to potentially improve performance.
Other Boosting Algorithms
While GBM is the most widely used, there are several other effective boosting algorithms:
-
AdaBoost (Adaptive Boosting) was one of the first successful boosting algorithms. It fits a sequence of weak learners on repeatedly modified versions of the data, thus giving higher influence to previously misclassified examples. The predictions are combined through a weighted majority vote.
-
XGBoost is an optimized distributed gradient boosting library designed to be highly efficient, flexible and portable. It implements machine learning algorithms under the Gradient Boosting framework, including GBDT, GBM, and Random Forests. XGBoost provides a parallel tree boosting that solves many data science problems in a fast and accurate way.
-
LightGBM is another gradient boosting framework that uses tree based learning algorithms. It is designed to be distributed and efficient with the following advantages: faster training speed and higher efficiency, lower memory usage, better accuracy, support of parallel and GPU learning, and capable of handling large-scale data.
-
CatBoost is a recently open-sourced machine learning algorithm from Yandex. It can easily integrate with deep learning frameworks like Google‘s TensorFlow and Apple‘s Core ML. Key advantages of CatBoost include its ability to handle categorical features automatically and great quality without parameter tuning.
In general, these newer boosting implementations aim to be faster, more flexible, and capable of handling larger datasets. However, the underlying boosting concepts remain similar.
Considerations with Boosting
While boosting algorithms tend to have excellent performance right out of the box, there are a few things to keep in mind:
-
Boosting can sometimes overfit, especially if the data is noisy. Early stopping, regularization, and cross-validation are important to prevent this.
-
Preparing the input data by normalizing or standardizing features can improve convergence and performance.
-
Feature engineering is still important. Adding relevant features and removing unnecessary ones can make a big difference.
-
Tuning hyperparameters like number of iterations, learning rate, and tree depth is required to get the best performance. Grid search or random search are commonly used.
-
Boosting tends to be slower to train than bagging methods like random forests, especially on larger datasets. The sequential nature of boosting limits its ability to parallelize.
Despite these considerations, boosting remains one of the top-performing algorithms for structured/tabular data tasks. As computing power increases and new optimizations are developed, boosting will continue to be a popular choice.
Explainable Boosting
As boosting models become more complex, explaining how they make predictions is increasingly important, especially in domains like healthcare. Recent techniques aim to make boosting models more interpretable:
-
SHAP (SHapley Additive exPlanations) values break down a prediction to show the impact of each feature. SHAP values can show the magnitude and direction of a feature‘s effect compared to the average prediction.
-
LIME (Local Interpretable Model-agnostic Explanations) is another method that fits a simpler, interpretable model locally around each prediction. For boosting models, LIME analyzes how the prediction changes when perturbing the input data.
-
Monotonicity constraints can be added to boosting algorithms to ensure the model‘s response to certain features aligns with domain knowledge. For example, enforcing that income has a positive monotonic effect on loan approval. This makes the model more stable and intuitive.
Tools like the R iml package and Python SHAP library make it easy to calculate and visualize these explanations for boosting models. As black-box models are increasingly deployed in high-stakes scenarios, clear explanations of their predictions will become standard.
Automated Boosting
Given the success of boosting, automated machine learning (AutoML) systems have made it a core part of their pipeline. H2O, an open-source machine learning platform, includes "XGBoost GBM" and "LightGBM" among its list of algorithms. Using H2O, it‘s possible to train, tune and interpret a boosting model with just a few lines of code.
Emerging techniques are also making it easier to apply boosting in new domains. Boosting algorithms for spatio-temporal data allow better forecasting of events over time and space. Boosting for survival analysis extends the technique to predicting time-to-event outcomes in the presence of censoring, a common situation in medical research. These developments will make boosting even more widely applicable.
Conclusion
Boosting is a powerful technique for building highly accurate models by combining many weak learners. Gradient boosting machines, in particular, have proven successful across a wide range of domains and datasets. By focusing each iteration on the mistakes of previous ones, boosting produces strong models that can generalize well.
Modern implementations of boosting like XGBoost and LightGBM make the technique faster and more scalable than ever. At the same time, methods for explaining and interpreting boosting models make them more transparent.
Boosting is a core part of the data scientist‘s toolkit, and new extensions continue to unlock its potential. Understanding the key concepts, algorithms and trade-offs involved will serve you well in making accurate predictions.