← 9. Prediction & machine learning
Gradient boosting
Try this analysis →

Gradient Boosting

An ensemble learning method that reduces overall prediction error by adding prediction models one after another.

Loss functionLearning rateEarly stoppingXGBoost, LightGBM, CatBoost

$500$400Actual $600Residual+$200$400$5740123456Trees

Suppose you built a decision tree to predict whether customers will cancel a service. You split customers by tenure and contract type, but some customers are still predicted wrongly. In particular, some customers with short tenure actually stay, and some long-term customers suddenly cancel.

What should you do if the first model can't adequately explain these differences? One way is to add a new model that learns the patterns the existing model missed. The best-known machine learning method that uses this principle is gradient boosting.

Gradient boosting is an ensemble learning method that reduces overall prediction error by adding prediction models one after another. It usually combines many small decision trees, training each new model to make up for what the models before it got wrong.

The best-known algorithms are the gradient boosting machine (GBM), XGBoost, LightGBM, and CatBoost. They all belong to the boosting family, but they differ in how they grow trees, how they handle categorical variables, and in features for training efficiency and controlling overfitting.

In this article we cover the basic concept of gradient boosting, how it learns, a real customer churn prediction example, the features of XGBoost, LightGBM, and CatBoost, the main hyperparameters, and how to interpret results and evaluate predictive performance.

SECTION 1

The concept of gradient boosting and how it learns

What is gradient boosting?

Gradient boosting is a machine learning method that builds a single prediction model by combining several weak learners one after another.

A weak learner is a model that is relatively simple on its own. Gradient boosting usually uses shallow decision trees, set up so that each tree contributes a little to the overall prediction.

For example, suppose you want to predict how much each customer will spend next month. At first you might predict a similar amount for every customer. But in reality some customers spend a lot and others spend little, so prediction errors occur.

Gradient boosting adds new trees in the direction that reduces this error. If the first tree has learned the relationship between customer tenure and spending, the next tree can additionally learn patterns that the existing model did not adequately explain. Repeating this process combines the predictions of many trees into a single model.

The important point is that gradient boosting is not simply a method of relearning only the cases it got wrong. The key is that it adds new models in the direction that reduces the chosen loss function.

How does gradient boosting work?

Gradient boosting improves its predictions by adding new models to the existing model one after another.

Take a regression problem that predicts a continuous value as an example. Suppose three customers actually spend $200, $400, and $600. At first, you can predict the average spending, $400, for all three customers.

CustomerActual spendingInitial predictionResidual
A$200$400−$200
B$400$400$0
C$600$400+$200
Actual $600$400$500$400$5740123456Number of trees
Step-by-step prediction correction

With a squared-error loss function, a new tree can learn in the direction that reduces the difference between the current predictions and the actual values. For example, suppose the first tree outputs corrections of −$160 for customer A, $0 for customer B, and +$160 for customer C.

Adding these corrections to the existing predictions can improve them. But gradient boosting does not necessarily apply the new tree's corrections in full. It usually applies a learning rate to control how much each tree contributes to the final prediction.

For example, with a learning rate of 0.1, only 10% of the new tree's corrections may be applied. Correcting a little at a time like this lets the prediction improve gradually as more trees are added.

In actual gradient boosting, the direction the next model learns is determined by the negative slope of the loss function, that is, the negative gradient. With squared-error loss this is tied to the residuals, but with the log loss used in classification problems, for example, it is computed differently.

SECTION 2

Bagging vs. boosting, and the loss function

How do bagging and boosting differ?

In ensemble learning, methods can be divided into bagging and boosting depending on how they combine multiple models.

Bagging creates several training samples, trains models on them relatively independently, and then combines their predictions. The best-known algorithm is the random forest. Boosting, by contrast, adds the next model sequentially, building on the models created before it.

Bagging · random forestAverage · voteBoosting · gradient boostingThe next tree learnsthe previous tree's errors
Bagging and boosting
CategoryBaggingBoosting
Main principleCombines the predictions of several modelsAdds models sequentially to reduce loss
How models are trainedModels trained relatively independentlyReflects the predictions of the previous step
Typical algorithmRandom forestGradient Boosting
Main featureReduces prediction varianceGradual improvement of predictions
Main settingsNumber of trees, number of split variablesLearning rate, number of trees, tree complexity

For example, in a random forest, many trees each predict whether a customer will cancel and the results are combined. Gradient boosting, on the other hand, adds new trees one after another to improve the predictions made by the existing model.

Gradient boosting is useful for learning complex patterns, but if training goes too far it can pick up even the noise in the training data. So settings such as the learning rate, number of trees, and maximum depth must be tuned appropriately.

Why does the loss function matter?

Gradient boosting learns in the direction that minimizes the loss function.

So the goal the model tries to learn depends on which loss function you use. For example, for a problem that predicts a continuous value such as spending, you can use mean squared error (MSE). For a problem that predicts one of two categories, such as whether a customer cancels, you can use log loss.

Prediction problemTypical loss functionMain goal
RegressionSquared-error lossReduce the difference between actual and predicted values
Binary classificationBinary log lossImprove the predicted probability of the actual class
Multiclass classificationMulticlass log lossImprove the predicted probabilities of several classes
Robust regressionAbsolute error, Huber loss, etc.Treat the influence of large errors differently

For example, when predicting customer churn, the model can learn not only to get cancel vs. stay right but also to assign high predicted probabilities to customers who actually cancel.

However, minimizing log loss does not necessarily mean the predicted probabilities are well calibrated. How closely the predicted probabilities match the actual event rates must be checked separately.

Also, not every loss function is supported in every gradient boosting implementation. In a real analysis, distinguish between the objective functions and the evaluation metrics supported by the library you chose.

SECTION 3

Main algorithms: XGBoost, LightGBM, CatBoost

What is XGBoost?

XGBoost (Extreme Gradient Boosting) is a widely used algorithm designed to train gradient boosting models efficiently and stably.

XGBoost supports regularization, which accounts for model complexity while reducing the loss function. It also uses various computational optimizations so that prediction models can be trained on data of many sizes.

Like ordinary gradient boosting, it adds trees one after another, but a distinctive feature is that it uses both the gradient and second-derivative information of the loss function when splitting trees and estimating leaf values.

Main features of XGBoost

XGBoost can be used for both classification and regression, and many settings can be tuned, such as tree depth, learning rate, row and column sampling, and regularization strength. It also supports learning the split direction for missing values and early stopping, among other things.

Because of these features, it is widely used in many kinds of structured data analysis, such as customer churn prediction, credit risk analysis, and sales forecasting.

However, XGBoost does not outperform other algorithms in every situation. Depending on the structure of the data and the settings, a random forest, LightGBM, CatBoost, or a simpler model can do better.

What is LightGBM?

LightGBM (Light Gradient Boosting Machine) is a gradient boosting algorithm designed to train efficiently on large datasets.

LightGBM's signature features are histogram-based splitting and leaf-wise tree growth.

What is leaf-wise growth?

There are several ways to grow a decision tree.

Depth-wiseLeaf-wiseSymmetric treeEvenly by levelOne branch grows deepSame split on each level
Tree growth strategies

Some tree growth strategies expand the tree level by level, around the nodes at the same depth. LightGBM's default leaf-wise strategy, by contrast, first splits whichever current leaf reduces the loss the most. This can help reduce the loss efficiently within a limited number of splits.

However, because some branches can grow much deeper than others, overfitting can occur when there is little data or the settings are inappropriate.

Main features of LightGBM

LightGBM can be expected to train efficiently on structured data with many observations and many predictors. It also provides its own handling of categorical variables.

However, there is no guarantee that it will be the fastest to train or the most accurate on every dataset. Actual performance depends on the size of the data, the makeup of the categorical variables, the hardware, the hyperparameters, and so on.

What is CatBoost?

CatBoost (Categorical Boosting) is a gradient boosting algorithm designed with a strong focus on handling categorical variables effectively.

For example, suppose you use variables such as contract type, region, product type, and usage channel to predict customer churn. These are categorical data, where the distinction between categories matters rather than the size of a number.

In typical machine learning analyses, categorical variables are often converted with methods such as one-hot encoding. CatBoost provides its own algorithms for handling categorical variables, which can reduce this preprocessing work.

Main features of CatBoost

CatBoost builds prediction models using statistics-based transformations of categorical variables and an ordered training procedure, among other techniques. In particular, it uses approaches designed to reduce the bias and leakage that can arise when target information is used in handling categorical variables.

It can also use a symmetric tree structure (oblivious or symmetric trees) as its default learning approach. Because of these features, it can be a useful choice for structured data with many categorical variables.

However, handling categorical variables automatically does not make data preparation or validation unnecessary. You still need to check the meaning of the variables, missing values, the number of categories, the structure of the data split, and so on.

SECTION 4

Comparing XGBoost, LightGBM, and CatBoost

How do XGBoost, LightGBM, and CatBoost differ?

All three algorithms belong to the gradient boosting family, but they differ in how they grow trees, how they process data, and in their computational optimizations.

CategoryXGBoostLightGBMCatBoost
Algorithm familyGradient BoostingGradient BoostingGradient Boosting
Main featureRegularization and many training settingsEfficient training and leaf-wise growthCategorical variable handling and ordered training
Typical tree growthVarious, depending on settingsLeaf-wise by defaultSymmetric trees by default
Categorical variable handlingBuilt-in support depending on version and settingsBuilt-in supportBuilt-in handling is a key feature
Classification and regressionBoth supportedBoth supportedBoth supported
Main considerationsComplexity of settingsPossible overfitting on small dataCategorical handling settings and training cost

Which algorithm is most appropriate depends on the size of the data, the makeup of the variables, and the prediction goal.

For example, for data with a very large number of categorical variables, you might try CatBoost first. If training efficiency matters on data with many observations, you can consider LightGBM, and if you want to use a variety of regularization and model settings, XGBoost can also be a good choice.

However, you should not conclude from these features alone that a particular algorithm will necessarily be faster or more accurate. In a real analysis, it is good to compare several candidate models under the same training and validation conditions and evaluation metrics.

SECTION 5

Customer churn prediction example and PR-AUC

Real-world case: predicting whether customers will cancel a service

Suppose a subscription service company uses data on 5,000 customers to predict whether they will cancel the service next month.

Of all the customers, 650 canceled and 4,350 stayed. The cancellation rate is 13%. The following variables were used in the analysis.

VariableData typeDescription
TenureNumericNumber of months using the service
Monthly feeNumericAmount paid each month
Contract typeCategoricalMonthly, annual, etc.
Service usage countNumericNumber of uses in the last 30 days
Customer service contactsNumericNumber of contacts in the last 90 days
Payment methodCategoricalCard, bank transfer, etc.
CanceledBinary categoricalCanceled / stayed

The research question is as follows.

"Which boosting algorithm performs best at predicting the likelihood of cancellation next month from customers' usage records?"

Suppose the data were split into 4,000 customers for training and 1,000 for final evaluation. The models' hyperparameters were chosen by cross-validation within the training data, and performance was checked on the final evaluation data at the end.

Predictive performance by model

AlgorithmROC-AUCPR-AUCLog loss
XGBoost0.870.530.286
LightGBM0.860.510.294
CatBoost0.880.550.279

These numbers do not represent the actual performance ranking of the algorithms.

ROC-AUCHigher is betterPR-AUCHigher is betterLog lossLower is betterXGBoost0.870.530.286LightGBM0.860.510.294CatBoost0.880.550.279
Performance comparison of the three algorithms

In this example, CatBoost had the highest ROC-AUC and PR-AUC and also the lowest log loss. So you can say that, of the three models, CatBoost showed relatively better results on this evaluation data.

However, the difference between XGBoost with an ROC-AUC of 0.87 and CatBoost with 0.88 is not large. In real research, you can account for the uncertainty due to variation in the evaluation sample and, if needed, also check confidence intervals or the results of repeated evaluations.

Also, if you keep checking the results on the final evaluation data and reselecting the algorithm, you can indirectly overfit to the evaluation data.

So it is good to choose the final model mainly based on validation results within the training data and to use the separately held-out evaluation data to check generalization performance.

Why does PR-AUC matter?

With data in which the event of interest is rare, such as customer churn, it is hard to judge predictive performance by accuracy alone.

In the case above, only 13% of all customers canceled. Even a model that predicts every customer will stay has 87% accuracy. But it doesn't find a single customer who actually cancels, so it is of little use for responding to churn.

In such cases it is good to also check precision, recall, PR-AUC, and so on. Precision is the share of customers predicted to be at risk of canceling who actually canceled, and recall is the share of customers who actually canceled that the model found.

PR-AUC summarizes the relationship between precision and recall across many classification thresholds. For example, in data with a 13% cancellation rate, the expected precision of a random ranking is about 13%. So when interpreting PR-AUC, you must also consider the overall positive rate.

However, a high PR-AUC does not mean that both precision and recall are high at a particular threshold. In actual operations, choose the classification threshold according to what action you will take with customers, and check performance at that threshold separately.

SECTION 6

Hyperparameters, learning rate, and early stopping

Which settings matter in gradient boosting?

The performance of a gradient boosting model is affected by its hyperparameter settings.

Typical ones include the learning rate, number of trees, maximum depth, minimum samples per leaf, row and column sampling rates, and regularization strength.

SettingMeaning
Learning rateHow much each new tree contributes to the final prediction
Number of treesHow many trees to add sequentially
Max depthLimits the complexity of each tree
Number of leavesLimits the number of leaves in each tree
Row sampling (subsampling)Share of observations used in training
Column samplingShare of variables used in training or splitting
L1 and L2 regularizationPenalties on coefficients, leaf values, etc.
Early stoppingStops training when validation performance stops improving

Whether a setting is supported and exactly what it means can differ by algorithm. For example, in LightGBM the number of leaves is one of the important settings, while in XGBoost and CatBoost tree depth and regularization settings can play an important role.

Hyperparameters should not simply be set high or low; they should be tuned to fit the data and the prediction goal.

How are the learning rate and the number of trees related?

In gradient boosting, the learning rate and the number of trees are closely related.

The learning rate controls how much each new tree contributes to the prediction. The number of trees determines how many trees are added sequentially during training. In general, lowering the learning rate means each tree corrects the prediction only a little, so more trees may be needed to reach a similar level of training performance.

Learning rate 0.30Learning rate 0.10Learning rate 0.03Number of trees →Loss
Loss reduction by learning rate

Conversely, raising the learning rate can reduce the loss quickly with few trees, but there is a risk that training proceeds too aggressively or that generalization performance drops. For example, you can compare settings like the following.

ModelLearning rateMaximum number of trees
A0.30100
B0.10300
C0.031,000

This does not mean the three settings perform the same.

In a real analysis, tune the learning rate and the number of trees together. In particular, early stopping lets you stop training at the point where validation performance no longer improves.

But the smallest learning rate and the most trees do not always produce the best results. You also need to consider training time, computing resources, and the structure of the data.

What is early stopping?

Gradient boosting reduces the loss on the training data as it adds trees one after another.

But even if the loss on the training data keeps going down, predictive performance on new data can start to get worse at some point. Early stopping is a way to mitigate this problem.

For example, suppose you set the model to add up to 1,000 trees, but PR-AUC on the validation data stops improving after the 250th tree. With early stopping, you can stop training when performance has not improved for a certain number of rounds, or choose the model from the point with the best validation performance.

Example of performance by number of trees

Number of treesTraining PR-AUCValidation PR-AUC
500.520.44
1500.710.51
2500.800.55
4000.890.53
6000.960.49
0.400.450.500.550.600.440.510.550.530.49Best · 250 trees50150250400600Number of treesValidation PR-AUC
Early stopping point

In this example, validation PR-AUC was highest with 250 trees. Adding more trees after that improved performance on the training data but lowered performance on the validation data. So early stopping helps cut unnecessary training and mitigate overfitting.

However, the validation data used for early stopping are part of the model training procedure. For the final performance evaluation, it is appropriate to use separate data that were not used for early stopping or model selection.

SECTION 7

Handling class imbalance

How do you handle class imbalance?

Gradient boosting is often used on data with class imbalance, such as customer churn, fraud detection, and rare-event prediction.

For example, suppose only 500 of 10,000 customers canceled the service. The cancellation rate is 5%, so even if the model predicts the majority class of staying customers well, it may not find enough of the customers who cancel.

In such cases you can consider class weights, positive-class weights, resampling, adjusting the classification threshold, and so on.

Class weights make prediction errors on the smaller class count more heavily in training. XGBoost has settings such as scale_pos_weight for binary classification problems, and LightGBM and CatBoost also support related weighting settings.

However, raising class weights does not always let you predict customers who actually cancel more accurately. Changing the weights can change not only the model's ranking performance but also how well its predicted probabilities are calibrated.

So choose appropriate metrics and thresholds depending on whether finding more customers who cancel matters more, or reducing unnecessary interventions does. Also, if you use resampling, apply it only within the training data so that information from the evaluation data does not leak.

SECTION 8

Interpreting variable importance

How do you check variable importance in gradient boosting?

Because gradient boosting models use the splits of many trees, variable importance can be computed.

But the meaning of the results depends on which importance method you use.

Split-based importance

This method counts how often a variable was used in tree splits. For example, if tenure was often used as a split condition across many trees, it may have high importance. However, being used often does not by itself establish that it contributed much to predictive performance.

Loss-reduction-based importance

This method computes how much the loss function or split criterion improved when a variable was used for a split. In XGBoost you can check importance measures such as Gain. This shows how much the variable contributed to improving predictions during splitting, but it is different from a causal effect on the actual outcome.

Permutation importance

This method randomly shuffles the values of a particular variable and computes how much the model's predictive performance drops. For example, if shuffling contract type lowered PR-AUC from 0.55 to 0.43, the original contract type information was probably important to the model's predictive performance.

However, if there are variables that are highly correlated with each other, the importance of individual variables can be distorted, so be careful.

SECTION 9

Explaining predictions with SHAP, PDP, and ALE

Can SHAP explain the prediction for an individual customer?

Because a gradient boosting model combines many trees, it is hard to explain its predictions with a single rule.

SHAP (SHapley Additive exPlanations) lets you look at how each variable contributed to the prediction for a particular observation. For example, suppose a customer's predicted probability of canceling is 72%.

If that customer has a short tenure, a monthly contract, and many customer service contacts, these characteristics may have contributed to raising the prediction score. Conversely, if the customer has used the service many times recently, that may have contributed to lowering the cancellation score.

SHAP helps explain these contributions by breaking them down by variable. However, SHAP values do not always directly represent increases or decreases in probability. In many tree-based classification models, SHAP values are computed on the log-odds or raw score scale.

So when interpreting SHAP results, check which output scale they were computed on. Also, SHAP values only explain the relationships the model used for its predictions; they do not have the causal meaning that actually changing a variable's value would change the outcome by the same amount.

What are PDP and ALE?

In gradient boosting, partial dependence plots (PDP) and accumulated local effects (ALE) can be used to look visually at the relationship between a variable and the predictions.

Partial dependence plot (PDP)

A PDP (partial dependence plot) shows how the model's average prediction changes as the value of a particular variable is varied. For example, you can change tenure to 3, 6, 12, and 24 months and see how the average predicted probability of canceling changes.

This lets you look at the relationships the model has learned, such as whether cancellation risk falls as tenure gets longer or whether the change is large in a particular range. But if tenure and contract type are strongly related, a limitation is that predictions may be computed using combinations of variables that are rare in reality.

Accumulated local effects (ALE)

ALE (accumulated local effects) computes the change in predictions within the actually observed range of a particular variable and accumulates these changes to summarize the variable's effect pattern. Compared with PDP, it can help reduce the influence of unrealistic combinations when variables are correlated.

However, ALE is also a way of explaining the predictive relationships the model has learned, not a method for estimating causal effects.

SECTION 10

Gradient boosting vs. random forest

Should you choose gradient boosting or a random forest?

Gradient boosting and random forests are both algorithms widely used for prediction on structured data.

Both combine many decision trees, but they differ in how they learn and in their main settings.

CategoryRandom forestGradient Boosting
Basic approachCombines the predictions of many treesAdds trees sequentially to reduce loss
Main goalReduces prediction varianceGradual reduction in prediction loss
Typical settingsNumber of trees, number of split variables, max depthLearning rate, number of trees, max depth, regularization
How models are trainedParallelizing across trees is relatively easySequential dependence between trees
Typical algorithmRandom ForestXGBoost, LightGBM, CatBoost

For example, if training time is limited and you want to start with simple hyperparameter tuning, you can use a random forest as a baseline model. On the other hand, if you want to explore high predictive performance while tuning many settings, you can compare gradient boosting as well.

But depending on the data, a random forest can perform better. So you should not judge which is better by the complexity of the algorithm alone. In practice, it is good to compare several candidate models using the same data split and evaluation metrics.

SECTION 11

Data leakage and cautions in validation

Why is data leakage dangerous in gradient boosting?

Because gradient boosting can learn complex combinations of variables, you need to be especially careful about data leakage.

Data leakage is the problem of including information in model training or evaluation that would not be available at the actual time of prediction. For example, if a model predicting whether customers will cancel next month included next month's cancellation request as an input variable, the model would be using information that is not available in real operations.

In such cases evaluation performance can look very high, but the model cannot be used to predict actual customers in advance. Leakage can also occur during preprocessing. If you impute missing values or select variables on the full data and then split it into training and evaluation data, information from the evaluation data can seep into the training process.

So split the data first, and determine preprocessing rules and model settings from the training data only. Also, if there are repeated observations of the same customer, consider splitting by group, and if you are predicting future outcomes, a time-based split may be appropriate.

SECTION 12

Reporting the results

How do you report gradient boosting results?

In prediction studies using gradient boosting, you should present not only the model name but also how the data were split, the input variables, the hyperparameter search process, the main evaluation metrics, and so on.

Using the customer churn prediction example above, you could write it as follows.

Example report

XGBoost, LightGBM, and CatBoost classification models were built to predict whether customers would cancel the service in the following month. Of the 5,000 customers in total, 4,000 were used for training and 1,000 for final evaluation. Each model's hyperparameters were selected by cross-validation within the training data. In the final evaluation, XGBoost had an ROC-AUC of 0.87 and a PR-AUC of 0.53, LightGBM had 0.86 and 0.51, and CatBoost had 0.88 and 0.55, respectively. In this example, CatBoost showed the highest ranking discrimination performance of the three models compared.

In real research, it is good to also report the class proportions in the evaluation data, the main settings used, the uncertainty in performance, and so on. Also, if you plan to use the predicted probabilities in actual operations, you should additionally check probability calibration and the precision, recall, and so on at the chosen classification threshold.

SECTION 13

Main application areas

Where can gradient boosting be used?

Gradient boosting is widely used for prediction problems on structured data in which many features are intertwined in complex ways.

Marketing and customer analytics

It can predict customers' likelihood of canceling a service, whether they will buy, how much they will spend on repeat purchases, customer lifetime value, and more. It is especially useful for problems in which interactions among several variables, such as contract type and tenure, matter.

Finance and economics

It can be used for loan delinquency risk, default risk, fraud detection, forecasting companies' financial performance, and more. In financial decision-making, however, the relevant regulations, model fairness, and explainability must also be considered.

Health and medical research

It can be used in research that predicts risk levels or particular health outcomes from multiple test results and patient characteristics. Applying it in actual clinical settings requires sufficient external validation and safety evaluation.

Education research

It can predict students' academic achievement, likelihood of dropping out, whether they will complete an educational program, and more.

Manufacturing and quality control

It can be used to predict whether products are defective, the likelihood of equipment failure, production quality, maintenance needs, and more.

Service operations and IT

It can predict the likelihood of system failures, customer inquiry volume, service usage patterns, whether transactions are anomalous, and more. As these examples show, gradient boosting can be used not only for classification but also for regression problems that predict continuous values, and it is useful for data that include a variety of numeric and categorical variables.

✓Wrap-up

Gradient boosting improves predictions by adding trees one after another.

Gradient boosting is an ensemble machine learning method that reduces overall loss by training prediction models sequentially and combining them.


Whereas a random forest focuses on reducing variability by combining the predictions of many trees, gradient boosting learns so that each new tree it adds improves the existing model's predictions.

The main implementations, XGBoost, LightGBM, and CatBoost, are all based on this principle, but they differ in how trees grow, how categorical variables are handled, and in training efficiency and regularization features.

Which algorithm is best depends on the size of the data, the characteristics of the variables, the evaluation metric, and the training environment. So compare several models under the same conditions, tune the learning rate, number of trees, and other settings appropriately, and check generalization performance on the final evaluation data.

Just as important as predictive accuracy is clearly defining which errors you want to reduce. When a particular event is rare, as with customer churn or fraud, it is good to look at PR-AUC, precision, recall, and so on together.

KEY TAKEAWAY

The core of gradient boosting is not simply combining many trees, but gradually learning the patterns that earlier models did not adequately explain to produce predictions that are useful on new data as well.