XGBoost and Random Forest® with Bayesian Optimisation
This article will explain how to use XGBoost and Random Forest with Bayesian Optimisation, and will discuss the main pros and cons of these methods.
By Edwin Lisowski, CTO at Addepto
Instead of only comparing XGBoost and Random Forest in this post we will try to explain how to use those two very popular approaches with Bayesian Optimisation and that are those models main pros and cons. XGBoost (XGB) and Random Forest (RF) both are ensemble learning methods and predict (classification or regression) by combining the outputs from individual decision trees (we assume tree-based XGB or RF).
Let’s dive deeper into comparison – XGBoost vs Random Forest
XGBoost or Gradient Boosting
XGBoost build decision tree one each time. Each new tree corrects errors which were made by previously trained decision tree.
Example of XGBoost application
At Addepto we use XGBoost models to solve anomaly detection problems e.g. in supervised learning approach. In this case XGB is very helpful because data sets are often highly imbalanced. Examples of such data sets are user/consumer transactions, energy consumption or user behaviour in mobile app.
Since boosted trees are derived by optimizing an objective function, basically XGB can be used to solve almost all objective function that we can write gradient out. This including things like ranking and poisson regression, which RF is harder to achieve.
XGB model is more sensitive to overfitting if the data is noisy. Training generally takes longer because of the fact that trees are built sequentially. GBMs are harder to tune than RF. There are typically three parameters: number of trees, depth of trees and learning rate, and the each tree built is generally shallow.
Random Forest (RF) trains each tree independently, using a random sample of the data. This randomness helps to make the model more robust than a single decision tree. Thanks to that RF is less likely to overfit on the training data.
Example of Random Forest application
The random forest dissimilarity has been used in a variety of applications, e.g. to find clusters of patients based on tissue marker data. Random Forest model is very attractive for this kind of applications in the following two cases:
Our goal is to have high predictive accuracy for a high-dimensional problem with strongly correlated features.
Our data set is very noisy and contains a lot of missing values e.g., some of the attributes are categorical or semi-continuous.
The model tuning in Random Forest is much easier than in case of XGBoost. In RF we have two main parameters: number of features to be selected at each node and number of decision trees. RF are harder to overfit than XGB.
The main limitation of the Random Forest algorithm is that a large number of trees can make the algorithm slow for real-time prediction. For data including categorical variables with different number of levels, random forests are biased in favor of those attributes with more levels.
Bayesian optimization is a technique to optimise function that is expensive to evaluate. It builds posterior distribution for the objective function and calculate the uncertainty in that distribution using Gaussian process regression, and then uses an acquisition function to decide where to sample. Bayesian optimization focuses on solving the problem:
The dimension of hyperparameters (x∈Rd) is often d < 20 in most successful applications.
Typically set A i a hyper-rectangle (x∈Rd:ai ≤ xi ≤ bi). The objective function is continuous which is required to model using Gaussian process regression. It also lacks special structure like concavity or linearity which make futile using techniques that leverage such structure to improve efficiency. Bayesian optimization consists of two main components: a Bayesian statistical model for modeling the objective function and an acquisition function for deciding where to sample next.
After evaluating the objective according to an initial space-filling experimental design they are used iteratively to allocate the remainder of a budget of N evaluations, as shown below:
- Observe at initial points
- While n ≤ N do
Update the posterior probability distribution using all available data
Let xn be a maximizer of the acquisition function
Observe yn = f(xn)
- Return a solution: the point evaluated with the largest
We can summarize this problem by saying that Bayesian optimization is designed for black-box derivative-free global optimization. It has become extremely popular for tuning hyperparameters in machine learning.
Below is a graphical summary of the whole optimization: Gaussian Process with posterior distribution along with observations and confidence interval, and Utility Function where the maximum value indicates the next sample point.
Thanks to utility function bayesian optimization is much more efficient in tuning parameters of machine learning algorithms than grid or random search techniques. It can effectively balance “exploration” and “exploitation” in finding global optimum.
To present Bayesian optimization in action we use BayesianOptimization  library written in Python to tune hyperparameters of Random Forest and XGBoost classification algorithms. We need to install it via pip:
pip install bayesian-optimization
Now let’s train our model. First we import required libraries:
We define a function to run Bayesian optimization given data, function to optimize and its hyperparameters:
We define function to optimize which is Random Forest Classifier and its hyperparameters n_estimators, max_depth and min_samples_split. Additionally we use the mean of cross validation score on given dataset:
Analogically we define a function and hyperparameters for XGBoost classifier:
Now based on chosen classifier we can optimize it and train the model:
As an example data we use a view [dbo].[vTargetMail] from AdventureWorksDW2017 SQL Server database where based on personal data we need to predict whether person buy a bike. As a result of Bayesian optimization we present consecutive function sampling:
As we can see Bayesian optimization found the best parameters in 23rd step which gives 0.8622 AUC score on test dataset. Probably this result can be higher if given more samples to check. Our optimized Random Forest model has ROC AUC curve presented below:
We presented a simple way of tuning hyperparameters in machine learning  using Bayesian optimization which is a faster method in finding optimal values and more sophisticated one than Grid or Random Search methods.
Bio: Edwin Lisowski is CTO at Addepto. He is an experienced advanced analytics consultant with a demonstrated history of working in the information technology and services industry. He is skilled in Predictive Modeling, Big Data, Cloud Computing and Advanced Analytics.
- How to Automate Hyperparameter Optimization
- Intro to XGBoost: Predicting Life Expectancy with Supervised Learning
- Random Forests vs Neural Networks: Which is Better, and When?