Skip to content## Understanding Boosted Trees Models

#### Boosting

#### Adaptive Boosting (AdaBoost)

#### AdaBoost Classifier in Python

#### Gradient Boosting

##### Regularization of Gradient Boosted Trees

#### scikit-learn Implementation

#### XGBoost

#### LightGBM

#### Concluding Remarks

## Similar Articles

— MachineLearning, Algorithms, Python — 16 min read

In the previous post, we learned about tree based learning methods - basics of tree based models and the use of bagging to reduce variance. We also looked at one of the most famous learning algorithms based on the idea of bagging- random forests. In this post, we will look into the details of yet another type of tree-based learning algorithms: boosted trees.

Boosting, similar to Bagging, is a general class of learning algorithm where a set of weak learners are combined to get strong learners.

For classification problems, a

weak learneris defined to be a classifier which is only slightly correlated with the true classification (it can label examples better than random guessing). In contrast, astrong learneris a classifier that is arbitrarily well-correlated with the true classification.

Recall that bagging involves creating multiple copies of the original training data set via bootstrapping, fitting a separate decision tree to each copy, and then combining all of the trees in order to create a single predictive model. Notably, each tree is built on a bootstrap data set, independent of the other trees.

In contrast, in Boosting, the trees are grown sequentially: **each tree is grown using information
from previously grown trees.** Boosting does not involve bootstrap sampling; instead each tree is
fit on a modified version of the original data set.

Unlike fitting a single large decision tree to the data, which amounts to *fitting the data hard*
and potentially **overfitting**, the boosting approach instead learns slowly. Given the current
model, a decision tree is fit to the residuals from the model. That is, a tree is fit using the
current residuals, rather than the outcome as the response.

The above concept of additively fitting weak learners to get a strong learner is not limited to tree based methods. In fact, we can choose these weak learners to be any model of our choice. In practice, however, decision trees or sometimes linear models are the most common choices. In this post, we will be focusing mainly on boosted trees - i.e. our choice of weak learners would be decision trees. Boosted trees are one of the most used models in on-line machine learning competitions like Kaggle.

Let us consider the case of boosted trees more rigorously. In particular, we will look at the case
of boosted decision trees for regression problems. To begin, set the resultant model
$\hat{F}(x) = 0$ and residual of the training response as the training target $y$. Now
**Sequentially**, for steps $b=1,2\ldots,B$ update $\hat{F}(x)$ as follows:
$\hat{F}(x)\leftarrow \hat{F}(x) + \lambda_b \hat{f}^{b}(x)$, where $\hat{f}^{b}(x)$ is a decision
tree learner fit on $(X, r)$ with $d$ splits (or $d+1$ terminal nodes) and $\lambda_b$ is a
weighting parameter. Also update residuals, $r$ as, $r \leftarrow r - \lambda_b \hat{f}^{b}(x)$. At
the end of $B$ steps, we will get the final model as
$\hat{F}(x) = \sum_{b=1}^{B}\lambda_b\hat{f}^{b}(x)$.

The above algorithm of boosted is quite generic. One can come with several strategies for how the decision trees are grown and fit on residuals, and for the choice of the weighting parameter $\lambda$. Therefore, there are various boosting methods. Some common examples are:

In this post, we will focus specifically on two of the most common boosting algorithms, AdaBoost and Gradient Boosting.

**Adaptive Boosting** or AdaBoost refers to a particular formulation of boosting where the idea of
residual learning is implemented through the concept of sample weights. In particular, at any
iteration, $b$, all training samples have a weight $w_i^b$ $\forall i = 1, \ldots N$, which is
equal to the current error $E(\hat{f}(x_i))$. The decision tree $\hat{f}^{b}(x)$ is fit to minimize
the error $E_b = \sum_{i=1}^{N}{E\big(\hat{F}(x) + \lambda_b \hat{f}^{b}(x)\big)}$. AdaBoost uses
an exponential error/loss function: $E(\hat{F}(x_i)) = e^{-y_i \hat{F}(x_i)}$. The choice of the
error function, $E()$, also determines the way weights $w_i^b$ and the weighting parameter
$\lambda_b$ are updated at different steps. A neat mathematical derivation of these quantities for
the case of binary classification can be found at the
Wiki article.

In particular case of the classification problems, we can also think of AdaBoost to be an algorithm where at any step, the misclassified samples get a higher weight than the correctly classified samples. The weights attached to samples are used to inform the training of the weak learner, in this case, decision trees to be grown in a way that favor splitting the sets of samples with high weights.

In practice, the above definition of the AdaBoost model is modified to include a learning rate
term, $\gamma$, as a regularization term that shrinks the contribution of all of the models. In
particular, the model is updated as,
$\hat{F}(x) \leftarrow \hat{F}(x) + \gamma \lambda_b \hat{f}^{b}(x)$. The main tuning parameters of
AdaBosst are, the **learning rate $\gamma$**, **number of estimators $B$**, and decision tree
related parameters like **depth of trees $d$**, and **number of samples per split** etc.

AdaBoost in Python

The python scikit-learn library implementations of AdaBoost (AdaBoostClassifier and AdaBoostRegressor) are based on a modified version of the AdaBoost algorithms, AdaBoost SAMME and SAMME.R. Stage-wise Additive Modeling using a Multi-class Exponential loss function (SAMME) algorithm provides an extension of AdaBoost for the case of multi-class classification. The SAMME.R (R forreal) variation of the algorithm is for prediction of weighted probabilities rather than the class itself.

Recall the US income data that we used in the
previous based post on tree based methods. In summary, in this dataset, we
are required to predict the income range of adults (<=50K or >50K) based on following features:
`Race`

, `Sex`

, `Education`

, `Work Class`

, `Capital Loss`

, `Capital Gain`

, `Relationship`

,
`Marital Status`

, `Age Group`

, `Occupation`

and `Hours of Work per Week`

. We have already seen
that, with the best decision tree model, we were able to achieve a prediction accuracy of 85.9%.
With the use of random forest models, we were able to increase the accuracy to 86.6%.

Let us try to solve the same problem using AdaBoost classifier from scikit-learn module. Please have a look at the previous post on tree-based methods to understand the EDA and preparation of the data.

`1from sklearn.ensemble import AdaBoostClassifier2aclf = AdaBoostClassifier(DecisionTreeClassifier(max_depth=5), n_estimators=100)3aclf.fit(x_train, y_train)4aclf.score(x_test, y_test)`

Without any parameter tuning, we see an accuracy of 85.54%!

Genetic Algorithms for Parameter Search

Once the number of hyper-parameters and the range of those parameter in models becomes large, the time required to find the best parameters becomes exponentially large using simple grid search. Instead of trying out every possible combination of parameters, we can evolve only the combinations that give the best results using Genetic Algorithms. A scikit-learn friendly implementation of this can be found in the sklearn-deap library. In many of the examples below we will be using this library to search for best model parameters.

The genetic algorithm itself has three main parameters:`population size`

,`tournament size`

and`no. of generations`

. Typically, for the problem of parameter search, we can use a population of 10-15 per hyper-parameter. The tournament size parameter affects the diversity of population being considered in future generation. This roughly translates into global vs local optimization for parameter search. In other words, if the tournament size is too large, we will have higher chance of getting a solution that is from local minima. A typical value of 0.1 times the population works well for the exercise of finding optimal model parameters. The number of generation decides the convergence of genetic algorithms. A larger value leads to better convergence but requires larger computation time.

Let us try using genetic algorithm to find optimal model parameters for AdaBoost classifier.

`1from evolutionary_search import EvolutionaryAlgorithmSearchCV23parameters = {4 'base_estimator__max_features':(11, 9, 6),5 'base_estimator__max_depth':(1, 2, 4, 8),6 'base_estimator__min_samples_split': (2, 4, 8),7 'base_estimator__min_samples_leaf': (16, 12, 8, 4),8 'n_estimators': (50, 100, 200, 500),9 'learning_rate': (1, 0.1, 0.01, 10)10}1112clf2 = EvolutionaryAlgorithmSearchCV(estimator=aclf,13 params=parameters,14 scoring="accuracy",15 cv=5,16 verbose=1,17 population_size=200,18 gene_mutation_prob=0.10,19 gene_crossover_prob=0.5,20 tournament_size=10,21 generations_number=100,22 n_jobs=8)23clf2.fit(x_train, y_train)`

This should take about **50 minutes** on a reasonable desktop machine! We can now use the best
parameters from this and create a new AdaBoost classifier.

`1aclf = AdaBoostClassifier(2 ecisionTreeClassifier(max_depth=4,3 max_features=11,4 min_samples_leaf=4,5 min_samples_split=2),6 n_estimators=100,7 learning_rate=0.1)8aclf.fit(x_train, y_train)9aclf.score(x_test, y_test)`

We see a significant improvement in our results with an accuracy of 87.06% on the testing data!

Given our data is highly imbalanced, let us look at the confusion matrix of our model on the test
data. Note that we are using the `confusion_matrix()`

method from the
previous post on tree based methods.

`1y_pred = aclf.predict(x_test)2cfm = confusion_matrix(y_test, y_pred, labels=[0, 1])3plt.figure(figsize=(10,6))4plot_confusion_matrix(cfm, classes=["<=50K", ">50K"], normalize=True)`

We find that our model has a much better predictive power (94.1%) for the dominant class (<=50K), while it has prediction rate of only 64.2% for the other class.

The approach used in the case of AdaBoost can be also viewed as an exponential loss minimization approach. Let us look at this mathematically for the case of a generic loss function, $L\big(y,\hat{F}(x)\big)$. The goal of the method is to find an $\hat{F}(x)$ that minimizes the average value of the loss function $L\big(y,\hat{F}(x)\big)$ on the training set. This can be achieved by starting with a constant function $\hat{F_0}(x)$, and incrementally expanding it in a greedy fashion as follows:

$\begin{gathered} \hat{F_0}(x) = \mathop{\arg\min}\limits_{\gamma} \sum_{i=1}^{N} L\big(y,\gamma \big) \\ \hat{F_b}(x) = \hat{F}_{b-1}(x) + \\ \mathop{\arg\min}\limits_{f \in \mathcal{H}} \sum_{i=1}^{N} L\big(y,\hat{F}_{b-1}(x) + f(x) \big) \\ \text{, for } b=1,2,\ldots, B \end{gathered}$where $f(x) \in \mathcal{H}$ refers to base learners, in this case tree models. The problem with this set up is that, it is computationally infeasible to choose optimal $f(x)$ at every step for an arbitrary loss function $L\big(y,\hat{F}(x)\big)$.

However, this can be simplified using a steepest descent approach. In this approach, at any step the decision tree model is trained on the pseudo-residuals, rather than residuals. The approach can be described in the following algorithm:

- Initialize model as, $\hat{F_0}(x) = \mathop{\arg\min}\limits_{\gamma} \sum_{i=1}^{N} L\big(y,\gamma \big)$
- for steps $b=1,2,\ldots, B$:
- compute pseudo-residuals as: $r_{ib} = -\Bigg[ \frac{\partial{L\big(y_i, \hat{F}_{b-1}(x_i)\big)}}{\partial{\hat{F}_{b-1}(x_i)}} \Bigg]$
- Fit a decision tree $\hat{f^b}(x)$ to pseudo-residuals, i.e. train it using the training set $(x_i, r_{ib})_{i=1}^N$
- Compute multiplier $\gamma_b$ using line search, where $0<\nu<1$ is the learning rate parameter $\gamma_b = \mathop{\arg\min}\limits_{\gamma} \sum_{i=1}^N L\big(y_i, \hat{F}_{b-1}(x) + \nu \gamma \hat{f^b}(x)\big)$
- update the model $\hat{F_b}(x) = \hat{F}_{b-1}(x) + \nu \gamma_b \hat{f^b}(x)$

In most real implementations of gradient boosted trees, rather than an individual tree weighing parameter $\gamma_b$, different parameters are used at different splits, $\gamma_{jb}$. If you recall from the last post, a decision tree model corresponds to diving the feature space in multiple rectangular regions and hence it can be represented as,

$\hat{f^b}(x) = \sum_{j=1}^{J} k_{jb} I\big(x\in R_{jb}\big)$where, $J$ is the number of terminal nodes (leaves), $I\big(x\in R_{jb}\big)$ is an indicator function which is 1 if $x\in R_{jb}$ and $k_{jb}$ is the prediction in $j^{th}$ leaf. Now, we can replace $\hat{f^b}(x)$ in above algorithm and replace $\gamma_b$ for the whole tree by $\gamma_{jb}$ per terminal node (leaf).

$\hat{F_b}(x) = \hat{F}_{b-1}(x) + \nu \sum_{j=1}^{J_b} \gamma_{jb} I\big(x\in R_{jb}\big)$where $\gamma_{jb}$ is given by the following line search,

$\gamma_{jb} = \mathop{\arg\min}\limits_{\gamma} \sum_{x_i \in R_{jb}} L\big(y_i, \hat{F}_{b-1}(x) + \gamma \big)$Here $J$ refers to the number of terminal nodes (leaves) in any of constituent decision trees. A
value of $J_b =2$, i.e. **decision stumps** means no interactions among feature variables are
considered. With a value of $J_b=3$ the model may include effects of the interaction between up to
two variables, and so on. Typically a value of $4 \le J_b \le 8$
work well for boosting.

Gradient Boosted Trees can be regularized by multiple approaches. Some common approaches are:

**Shrinkage / Learning Rate:**For each gradient step, the step magnitude is multiplied by a factor between 0 and 1 called a learning rate ($0 <\nu < 1$). In other words, each gradient step is shrunken by some factor $\nu$. The rational for this to work as a regularization parameter has never been clear to me. My personal take is the shrinkage enables us to use a different prior. Telgarsky et al. provide a mathematical proof that shrinkage makes gradient boosting to produce an approximate maximum margin classifier, i.e. a classifier which is able to maximize the associated distance from the decision boundary for each example.**Sub-Sampling:**Motivated by the bagging method, at each iteration of the algorithm, a decision tree is fit on a subsample of the training set drawn at random without replacement. Also, like in bagging, sub-sampling allows one to define an out-of-bag error of the prediction performance improvement by evaluating predictions on those observations which were not used in the building of the next base learner. Out-of-bag estimates help avoid the need for an independent validation dataset, but often underestimate actual performance improvement and the optimal number of iterations.**Minimum sample size for splitting trees**, and**Minimum sample size for tree leaves:**It is used in the tree building process by ignoring any splits that lead to nodes containing fewer than this number of training set instances. Imposing this limit helps to reduce variance in predictions at leaves.**The number of trees or Boosting Iterations, $B$**Increasing $B$ reduces the error on training set, but setting it too high may lead to over-fitting. An optimal value of $B$ is often selected by monitoring prediction error on a separate validation data set.**Sampling Features:**We can apply the idea of randomly choosing small subsets of features for different trees, as in the case of Random Forest models.

scikit-learn provides a simple and generic implementation of the above described algorithm that is valid of different types of loss functions. Below is a simple implementation for the case of income data.

`1from sklearn.ensemble import GradientBoostingClassifier23params = {'n_estimators': 200, 'max_depth': 4, 'subsample': 0.75,4 'learning_rate': 0.1, 'min_samples_leaf': 4, 'random_state': 3}5gclf = GradientBoostingClassifier(**params)67gclf.fit(x_train, y_train)8gclf.score(x_test, y_test)`

Turns out we have got a quite good model just by chance! This has an accuracy of 87.11% on our test data!

We could try finding optimal parameters for this as before. However, in my experience this is a very academic implementation of boosted trees. Other implementation like XGBoost, LightGBM and CatBoost are more optimized implementations and hence we will focus our tuning for some of these libraries only.

The biggest drawback of the gradient boosting trees is that the algorithm is quite sequential in nature and hence are very slow and they can not take advantage of advanced computing features for parallelization like multiple threads and cores, GPU etc.

XGBoost is a python (C++ and R as well) library that provides an optimized implementation of gradient boosted trees. It uses various tricks like regularization of number of leaves and leaf weights, sparsity aware split finding, column block for parallel learning and cache-aware access. Using these tricks the implementation provides a parallelized efficient version of the algorithm. The details of these can be found here. XGBoost is one of the most famous machine learning libraries used in on-line machine learning competitions like Kaggle.

$\text{Loss Term at step b is:} \\ \sum_{i=1}^{N} L\big( y_i, \hat{F_b}(x_i) \big) + \sum_{k=1}^b \Big (\eta J_k + \frac{1}{2} \lambda \left\lVert \gamma_{jk} \right\rVert^2 \Big )$

Regularization: Apart from regular gradient boosted trees, XGBoost provides two additional types of regularization, by adding $L_1$ constraints on number of leaves ($J_b$) and $L_2$ constraints on the leaf weights ($\gamma_{jb}$) to the loss function. Mathematically, The loss function is modified as follows:Here, the second term in the loss function, penalizes the complexity of the model, i.e. decision tree functions.

Additional Weak Learners: Apart from decision trees, XGBoost also supports linear models and DART (decision trees with dropout) as weak learners. In the DART algorithm, only a subset of available trees are considered in calculating the pseudo-residuals on which the new trees are fit.

XGBoost has many parameters that control the fitting of the model. Below are some of the relevant
parameters and tuning them would be helpful in the most common cases. *Please note that original
XGBoost library parameters might have a different name than before, since I am using the
scikit-learn API parameter names below.*

**max_depth (default=3)**: Maximum depth of a tree, increase this value will make the model more complex / likely to be over-fitting. A value of 0 indicates no limit.*A Typical Value in the range of 2-10 can be used for model optimization*.**n_estimators (default=100)**: The number of boosting steps to perform. This can also be seen as number of trees to be used in the boosted model. This number is inversely proportional to the learning*rate (eta) parameter, i.e. if we use a smaller value of learning_rate, n_estimators has be made larger. _A Typical Value in the range of $\ge 100$ can be used for model optimization. However, it is best to used XGBoost built-in*.`cv()`

method to find this parameter (See the example ahead)**min_child_weight (default=1)**: The minimum sum of instance weight needed in a child node. If the tree partition step results in a leaf node with the sum of instance weight less than the min*child_weight, then any further partitioning will be stopped. The larger the value of this parameter, the more conservative the algorithm will be. _A Typical Value in the range of 1-10 can be used for model optimization*.**learning_rate (default=0.1)**: The step size shrinkage used to prevent over-fitting. After each boosting step, we can directly get the weights of new features. and the learning*rate parameter (also referred as*.**eta**in regular XGBoost API) shrinks the feature weights to make the boosting process more conservative (See above formulation of Gradient Boosted Trees for mathematical details). _A Typical Value in the range of 0.001-0.3 can be used for model optimization**gamma (default=0)**: (Also referred as**min_split_loss**in regular XGBoost API) The minimum loss reduction required to make a further partition on a leaf node of the tree. The larger, the more conservative the algorithm will be.*The value of this parameter depends on the type of loss function being used. A Typical Value in the range of 0.0-0.7 can be used for model optimization*.**reg_alpha (default=0)**: (Also referred as**alpha**in regular XGBoost API) L1 regularization term on weights, increasing this value will make model more conservative (see XGBoost paper for mathematical details).*A Typical Value in the range of 0-1 can be used for model optimization*.**reg_lambda (default=1)**: (Also referred as**lambda**in regular XGBoost API) L2 regularization term on weights, increasing this value will make model more conservative (see XGBoost paper for mathematical details).*A Typical Value in the range of 0-1 can be used for model optimization*.**subsample (default=1)**: Subsample ratio of the training instance. Setting it to 0.5 means that XGBoost randomly collected half of the data instances to grow trees. This parameter is used to prevent over-fitting.*A Typical Value in the range of 0.5-1.0 can be used for model optimization*.**colsample_bytree (default=1)**: Subsample ratio of columns when constructing each tree. Similar to Random Forest models, models tend to be more generalizable, if a number between 0 and 1 is used.*A Typical Value in the range of 0.5-1.0 can be used for model optimization*.**scale_pos_weight (default=1)**: Controls the balance of positive and negative class weights, useful for unbalanced class problems.*A typical value to consider: sum(negative cases) / sum(positive cases)*.

Apart from above set of parameters, there are several parameters that should also be considered
while tuning. Some examples of such parameters are: **objective** (objective function/ loss
function to use, depends on problem, for eg. binary vs. multi-class classification),
**tree_method** (The tree construction algorithm used in XGBoost(see description in the
paper), **random_state** (seed for random number
generator), **n_jobs** (number of threads to use to train the model) and many other parameters
related to different types of tree methods used.
This article
can be used as a good general reference for tuning XGBoost models.

Let us tune XGBoost model for our problem of income prediction. A simple sklearn API implementation can be used as below.

`1import xgboost as xgb23params = {4 'n_estimators': 100,5 'max_depth': 6,6 'subsample': 0.75,7 'learning_rate': 0.1,8 'min_samples_split': 2,9 'min_samples_leaf': 8,10 'random_state': 32,11 'objective': 'binary:logistic',12 'n_jobs': 813 }14xclf = xgb.XGBClassifier(**params)1516xclf.fit(x_train, y_train)17xclf.score(x_test, y_test)`

With this reasonable guess of parameters based on previous models, we already see an accuracy of 86.75%.

Let us try to find the optimal parameters for the XGBoost model. If we simply try to do a brute force grid search, it can be computationally very expensive and unreasonable on a desktop. Here is a sample parameters list that can give us an idea of what such a grid search could look like.

`1independent_params = {2 'random_state': 32,3 'objective': 'binary:logistic',4 'n_jobs': 8,5}6params = {7 'n_estimators': (100, 200, 400, 800, 1000),8 'max_depth': (4, 6, 8),9 'subsample': (0.75, 0.8, 0.9, 1.0),10 'learning_rate': (0.1, 0.01, 0.05),11 'colsample_bytree': (0.75, 0.8, 0.9, 1.0),12 'min_child_weight': range(1,6,2),13 'reg_alpha': [i/10.0 for i in range(0,5)],14 'gamma':[i/10.0 for i in range(0,5)],15 'reg_lambda': (1, 0.1, 10)16 }`

If we try to do a grid search of this with 5-fold cross validation, it will involve a whopping 0.81 million model training calls! And, even this will not be enough, as we will need additional model training steps to fine-tune our parameter search for finer and/or different range of parameters. Clearly, we need a different approach to solve this.

I will take an approach of optimizing different set of parameters in batches. To begin, we will
choose a fixed learning rate of 0.1, and n_estimators=200. We will try to find only tree related
parameters (i.e. **max_depth**, **gamma**, **subsample** and **colsample_bytree**) using grid
search or genetic algorithm.

`1ind_params = {2 'random_state': 32,3 'objective': 'binary:logistic',4 'n_estimators': 200,5 'learning_rate': 0.1,6}7params = {8 'max_depth': (4, 6, 8),9 'subsample': (0.75, 0.8, 0.9, 1.0),10 'colsample_bytree': (0.75, 0.8, 0.9, 1.0),11 'gamma': [i/10 for i in range(0,5)]12}1314clf2 = EvolutionaryAlgorithmSearchCV(15 estimator=xgb.XGBClassifier(**ind_params),16 params=params,17 scoring="accuracy",18 cv=5,19 verbose=1,20 population_size=60,21 gene_mutation_prob=0.10,22 gene_crossover_prob=0.5,23 tournament_size=5,24 generations_number=100,25 n_jobs=8)26clf2.fit(x_train, y_train)`

This gives us the following optimal values for different parameters:

`1Best individual is: {'max_depth': 6, 'subsample': 1.0, 'colsample_bytree': 0.8, 'gamma': 0.2}2with fitness: 0.8710727557507447`

We can do a finer grid search to get more precise values. For this exercise, let us move on to the next stage of parameter tuning of XGBoost.

XGBoost provides an optimized version of cross validation using `cv()`

method which supports early
stopping to give us optimal value of **n_estimators**.

`1xgb1 = xgb.XGBClassifier(2 learning_rate =0.1,3 n_estimators=10000,4 max_depth=6,5 min_child_weight=1,6 gamma=0.2,7 subsample=1.0,8 colsample_bytree=0.8,9 objective= 'binary:logistic',10 n_jobs=8,11 scale_pos_weight=1,12 random_state=32)13xgb_param = xgb1.get_xgb_params()14xgtrain = xgb.DMatrix(x_train, label=y_train)15cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=xgb1.get_params()['n_estimators'], nfold=5, metrics='auc', early_stopping_rounds=50)1617print("Number of Predicted n_estimators = ", cvresult.shape[0])`

This gives us a value of **n_estimators = 206**. We will now use these parameters to search for the
next set of tree building parameters: **max_depth** and **min_child_weight**.

`1params = {2 'max_depth':range(3,10,2),3 'min_child_weight':range(1,6,2)4}56ind_params = {'learning_rate': 0.1,7 'n_estimators': 206,8 'gamma': 0.2,9 'subsample': 1.0,10 'colsample_bytree': 0.8,11 'objective': 'binary:logistic',12 'random_state': 32}1314clf2 = EvolutionaryAlgorithmSearchCV(15 estimator=xgb.XGBClassifier(**ind_params),16 params=params,17 scoring="accuracy",18 cv=5,19 verbose=1,20 population_size=30,21 gene_mutation_prob=0.10,22 gene_crossover_prob=0.5,23 tournament_size=5,24 generations_number=100,25 n_jobs=826 )27clf2.fit(x_train, y_train)`

The optimal set of parameters found by this are:

`1Best individual is: {'max_depth': 7, 'min_child_weight': 1}2with fitness: 0.8712877368631184`

We can now use these parameters as fixed values and optimize regularization parameters:
**reg_alpha** and **reg_lambda**.

`1ind_params = {'learning_rate': 0.1,2 'n_estimators': 206,3 'gamma': 0.2,4 'subsample': 1.0,5 'colsample_bytree': 0.8,6 'objective': 'binary:logistic',7 'random_state': 32,8 'max_depth': 7,9 'min_child_weight': 1}1011params = {'reg_alpha':[0, 0.001, 0.005, 0.01, 0.05], 'reg_lambda':[0.01, 0.1, 1, 10, 100]}1213clf2 = EvolutionaryAlgorithmSearchCV(14 estimator=xgb.XGBClassifier(**ind_params),15 params=params,16 scoring="accuracy",17 cv=5,18 verbose=1,19 population_size=30,20 gene_mutation_prob=0.10,21 gene_crossover_prob=0.5,22 tournament_size=3,23 generations_number=100,24 n_jobs=825 )26clf2.fit(x_train, y_train)`

The optimal set of parameters found by this search are:

`1Best individual is: {'reg_alpha': 0.001, 'reg_lambda': 1}2with fitness: 0.8714720063880101`

We can now decrease the learning rate by an order to magnitude to get a more stable model. However,
we will also need to find the optimal value of number of estimators again using the `cv()`

method.

`1ind_params = {'learning_rate': 0.01,2 'n_estimators': 5000,3 'gamma': 0.2,4 'reg_alpha': 0.001,5 'reg_lambda': 1,6 'subsample': 1.0,7 'colsample_bytree': 0.8,8 'objective': 'binary:logistic',9 'random_state': 32,10 'max_depth': 7,11 'n_jobs': 8,12 'min_child_weight': 1}1314xgb2 = xgb.XGBClassifier(**ind_params)15xgb_param = xgb2.get_xgb_params()16xgtrain = xgb.DMatrix(x_train, label=y_train)17cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=xgb1.get_params()['n_estimators'], nfold=5, metrics='auc', early_stopping_rounds=50)1819print("Number of Predicted n_estimators = ", cvresult.shape[0])`

We get an optimal value of **n_estimators = 1559**. Let us use now all of these optimized values to
make a final XGBoost model.

`1ind_params = {'learning_rate': 0.01,2 'n_estimators': 1559,3 'gamma': 0.2,4 'reg_alpha': 0.001,5 'reg_lambda': 1.0,6 'subsample': 1.0,7 'colsample_bytree': 0.8,8 'objective': 'binary:logistic',9 'random_state': 32,10 'max_depth': 7,11 'n_jobs': 8,12 'min_child_weight': 1}1314xclf = xgb.XGBClassifier(**ind_params)1516xclf.fit(x_train, y_train)17xclf.score(x_test, y_test)`

We get test accuracy of 86.99%. Now, this seems odd. After we did all this computation and we still have got a test accuracy that is smaller than an optimized version of scikit-learn's Gradient Boosting Tree implementation. However, if you have paid attention to the metric, you can notice - for cross validation, I started using 'auc' as metric, instead of 'accuracy'. This will give us better accuracy for the less abundant class (> 50K salary) but at the cost of slight decrease in the accuracy of the more abundant class (<= 50K salary). We can verify this by the following confusion matrix plot.

`1y_pred = xclf.predict(x_test)2cfm = confusion_matrix(y_test, y_pred, labels=[0, 1])3plt.figure(figsize=(10,6))4plot_confusion_matrix(cfm, classes=["<=50K", ">50K"], normalize=True)`

We see the largest accuracy of the less abundant class with an accuracy of 64.9%, compared to the previous best of 64.2%. We can also look the importance of different features for this XGBoost model.

`1importances = xclf.feature_importances_2indices = np.argsort(importances)3cols = [cols[x] for x in indices]4plt.figure(figsize=(10,6))5plt.title('Feature Importances')6plt.barh(range(len(indices)), importances[indices], color='b', align='center')7plt.yticks(range(len(indices)), cols)8plt.xlabel('Relative Importance')`

Similar to XGBoost, LightGBM is another optimized implementation of Gradient Boosting developed by Microsoft (similar to XGBoost available in Python, C++ and R). The main difference between LightGBM and other Gradient boosted trees (like XGBoost) implementations is in the way trees are grown. The details of this can be found on their features page. Briefly, LightGBM splits the tree leaf-wise with the best fit whereas other boosting algorithms split the tree depth-wise or level-wise. The two approaches can be best visualized in the following illustrations:

**Level-wise Splits:**

**Leaf-wise Splits:**

Leaf-wise splits lead to increase in complexity and may lead to over-fitting, and hence extra
caution needs to be taken in tuning. Some of the biggest advantages of LightGBM over XGBoost is in
terms of extremely fast training speed, lower memory usage, compatibility with large datasets and
highly parallel computational support using threads, MPI and GPUs. LightGBM also has inbuilt
support for categorical variables, unlike XGBoost, where one has to pre-process the data to convert
all of the categorical features to integer ones using *one-hot encoding* or *label encoding*.

Since the trees are grown differently in LightGBM, its tuning procedure is quite different than XGBoost. Note that the latest version of XGBoost also provides a tree building strategy (depth-wise) which is quite similar to LightGBM.

All the parameters described above for XGBoost are also valid for LightGBM library. However, some parameters can have different names. Given the strategy of growing trees is different in LightGBM, an additional parameter needs to be tuned as well.

**num_leaves**: Maximum tree leaves for base learners. Note that since trees are grown depth-first,
this parameters is independent of the **max_depth** parameter and has to be tuned independently. A
typical value for starting should be much less than 2^{max _depth}.

When using the scikit-learn API of LightGBM, one should keep in mind that some of the parameter
names are not standard ones (even though described in the API reference). In particular, I found
that *seed* and *nthreads* as parameters, instead of *random*_*state* and *n*_*jobs*,
respectively.

Let us tune a LightGBM model for the problem of Income prediction.

`1import lightgbm as lgb23params = {4 'n_estimators': 100,5 'num_leaves': 48,6 'max_depth': 6,7 'subsample': 0.75,8 'learning_rate': 0.1,9 'min_child_samples': 8,10 'seed': 32,11 'nthread': 812}13lclf = lgb.LGBMClassifier(**params)1415lclf.fit(x_train, y_train)16lclf.score(x_test, y_test)`

This results in test accuracy of 86.8%.

Given this library also has many parameters, similar to XGBoost, we need to use a similar strategy of tuning in stages.

First we will fix learning rate to a reasonable value of 0.1 and number of estimators = 200, and
tune only the major tree building parameters: `max_depth`

, `subsample`

, `colsample_bytree`

and
`num_leaves`

. We will use the genetic algorithm to search for optimal values of these parameters.

`1ind_params = {2 'seed': 32,3 'n_estimators': 200,4 'learning_rate': 0.1,5 'nthread': 16}7params = {8 'max_depth': (4, 6, 8),9 'subsample': (0.75, 0.8, 0.9, 1.0),10 'colsample_bytree': (0.75, 0.8, 0.9, 1.0),11 'num_leaves': (12, 16, 36, 48, 54, 60, 80, 100)12}1314clf2 = EvolutionaryAlgorithmSearchCV(15 estimator=lgb.LGBMClassifier(**ind_params),16 params=params,17 scoring="accuracy",18 cv=5,19 verbose=1,20 population_size=50,21 gene_mutation_prob=0.10,22 gene_crossover_prob=0.5,23 tournament_size=5,24 generations_number=100,25 n_jobs=826 )27clf2.fit(x_train, y_train)`

This gives the following set of optimal parameters:

`1Best individual is: {'max_depth': 6, 'subsample': 1.0, 'colsample_bytree': 0.75, 'num_leaves': 54}2with fitness: 0.870888486225853`

Now, we can use grid search to fine tune the search of number of leaves parameter.

`1ind_params = {2 'seed': 32,3 'n_estimators': 200,4 'learning_rate': 0.1,5 'nthread': 1,6 'max_depth': 6,7 'subsample': 1.0,8 'colsample_bytree': 0.75,9}10params = {11 'num_leaves': (48, 50, 52, 54, 56, 58, 60)12 }1314clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)15clf2.fit(x_train, y_train)16print(clf2.best_params_)`

Similar to XGBoost, LightGBM also provides a `cv()`

method that can be used to find optimal value
of number of estimators using early stopping strategy. Another strategy would be to search for this
parameter as well. In the following, I want to use this grid search strategy to find best value of
number of estimators, just to show how tedious this can be!

`1ind_params = {2 'seed': 32,3 'learning_rate': 0.1,4 'nthread': 1,5 'max_depth': 6,6 'subsample': 1.0,7 'colsample_bytree': 0.75,8 'num_leaves': 549}10params = {'n_estimators': (200,300,400,800,1000)}1112clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)13clf2.fit(x_train, y_train)14print(clf2.best_params_)1516params = {'n_estimators': (250,275,300,320,340,360,380)}1718clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)19clf2.fit(x_train, y_train)20print(clf2.best_params_)2122params = {'n_estimators': (322,324,325,326,327,328,330,332,334,336,338)}2324clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)25clf2.fit(x_train, y_train)26print(clf2.best_params_)`

We find that the optimal value of `n_estimators`

to be 327.

Now, we can use the similar strategy to find and fine-tune the best regularization parameters.

`1ind_params = {2 'seed': 32,3 'learning_rate': 0.1,4 'n_estimators': 327,5 'nthread': 1,6 'max_depth': 6,7 'subsample': 1.0,8 'colsample_bytree': 0.75,9 'num_leaves': 5410}1112params = {'reg_alpha' : [0,0.1,0.5,1],'reg_lambda' : [1,2,3,4,6],}1314clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)15clf2.fit(x_train, y_train)16print(clf2.best_params_)1718params = {'reg_alpha' : [0.2,0.3,0.4,0.5,0.6,0.7,0.9],'reg_lambda' : [1.5,2,2.5],}19clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)20clf2.fit(x_train, y_train)21print(clf2.best_params_)2223params = {'reg_alpha' : [0.55,0.58,0.6,0.62,0.65,0.68],'reg_lambda' : [1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9],}24clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)25clf2.fit(x_train, y_train)26print(clf2.best_params_)2728params = {'reg_alpha' : [0.61,0.62,0.63,0.64],'reg_lambda' : [1.85,1.88,1.9,1.95,1.98],}29clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)30clf2.fit(x_train, y_train)31print(clf2.best_params_)`

Finally, we can decrease the learning rate to 0.01 and find the optimal value of `n_estimators`

.

`1ind_params = {2 'seed': 32,3 'learning_rate': 0.01,4 'nthread': 1,5 'max_depth': 6,6 'subsample': 1.0,7 'colsample_bytree': 0.75,8 'num_leaves': 54,9 'reg_alpha': 0.62,10 'reg_lambda': 1.911}12params = {'n_estimators': (3270,3280,3300,3320,3340,3360,3380,3400)}1314clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)15clf2.fit(x_train, y_train)16print(clf2.best_params_)1718params = {'n_estimators': (3325,3330,3335,3340,3345,3350,3355)}1920clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)21clf2.fit(x_train, y_train)22print(clf2.best_params_)2324params = {'n_estimators': (3326,3327,3328,3329,3330,3331,3332,3333,3334)}2526clf2 = GridSearchCV(lgb.LGBMClassifier(**ind_params), params, cv=5, n_jobs=8, verbose=1)27clf2.fit(x_train, y_train)28print(clf2.best_params_)`

We find the optimal `n_estimators`

to be equal to 3327 for a learning rate of 0.01. We can now
built a final LightGBM model using these parameters and evaluate the test data.

`1ind_params = {2 'seed': 32,3 'learning_rate': 0.01,4 'n_estimators': 3327,5 'nthread': 8,6 'max_depth': 6,7 'subsample': 1.0,8 'colsample_bytree': 0.75,9 'num_leaves': 54,10 'reg_alpha': 0.62,11 'reg_lambda': 1.912}13lclf = lgb.LGBMClassifier(**ind_params)14lclf.fit(x_train, y_train)15lclf.score(x_test, y_test)`

We get a test accuracy of 87.01%. Similar to previous cases, we can again look at the accuracy of individual classes using the following confusion matrix plot:

`1y_pred = lclf.predict(x_test)2cfm = confusion_matrix(y_test, y_pred, labels=[0, 1])3plt.figure(figsize=(10,6))4plot_confusion_matrix(cfm, classes=["<=50K", ">50K"], normalize=True)`

We find the results to be slightly better than XGBoost with 65% accuracy of the less abundant >50K salary class. We can also look the importance of different features in this model:

`1importances = lclf.feature_importances_2indices = np.argsort(importances)3cols = [cols[x] for x in indices]4plt.figure(figsize=(10,6))5plt.title('Feature Importances')6plt.barh(range(len(indices)), importances[indices], color='b', align='center')7plt.yticks(range(len(indices)), cols)8plt.xlabel('Relative Importance')`

XGBoost has been one of the most famous libraries used to win several machine learning competitions on Kaggle and similar sites. Slowly, LightGBM is also gaining traction due to its speed and parallelization capabilities. CatBoost is another boosting library from Yandex that has been shown to be quite efficient. I have used it personally yet though. If I find it to be worth making a move to, I will write about it in a future post.