The Gradient Boosting Decision Tree (GBDT), as a leader in ensemble learning, has won the favor of many data scientists due to its excellent performance in classification and regression tasks. This article will take you deep into the mysterious veil of GBDT and demonstrate how to efficiently implement GBDT using the sklearn library.
1. What is GBDT
Before introducing GBDT, let’s first introduce Boosting Trees.
Boosting Trees are an enhancement method using classification trees or regression trees as the base classifiers. Boosting Trees are considered one of the best-performing methods in statistical learning.
The boosting method actually uses an additive model (i.e., a linear combination of base functions) and a forward stagewise algorithm. When decision trees are used as base functions, the boosting method is called Boosting Trees. For classification problems, the decision tree is a binary classification tree, and for regression problems, it is a binary regression tree.

Boosting Trees use an additive model and a forward stagewise algorithm to optimize the learning process. When the loss function is the squared loss or exponential loss function, each step of optimization is straightforward. However, for general loss functions, each step of optimization is often not so easy. To address this issue, Friedman proposed the Gradient Boosting algorithm. It transforms the problem into finding the fastest descent direction on the gradient of the loss function and approximates the solution.
GBDT is similar to Boosting Trees, with the model still being an additive model and the learning algorithm being a forward stagewise algorithm. The difference is that GBDT does not specify the type of loss function, denoted as .
Gradient Boosting is a major class of algorithms in Boosting, inspired by the gradient descent method. Its basic principle is “to train new weak classifiers based on the negative gradient information of the current model’s loss function”, and then combine the trained weak classifiers into the existing model in an additive manner. The Gradient Boosting algorithm that uses decision trees as weak classifiers is called GBDT.
2. Implementation of GBDT in sklearn
Let’s see how to implement the GBDT algorithm using the scikit-learn
library.
Gradient Tree Boosting or Gradient Boosted Decision Trees (GBDT) is a generalization of Boosting for any differentiable loss function. GBDT is an accurate and effective off-the-shelf program that can be used for regression and classification problems in various fields, including web search, ranking, and ecology.
The ensemble module sklearn.ensemble
provides methods for classification and regression through Gradient Boosting Trees.
Note: Inspired by LightGBM (see [[LightGBM]](http://scikit-learn.org.cn/view/90.html#1.11.6 Voting Classifier)), Scikit-learn 0.21 introduced two new experimental implementations of Gradient Boosting Trees, namely HistGradientBoostingClassifier and HistGradientBoostingRegressor. When the number of samples exceeds tens of thousands, these histogram-based estimators can be several orders of magnitude faster than
GradientBoostingClassifier
andGradientBoostingRegressor
. They also have built-in support for missing values, thus avoiding the need for imputation. These estimators will be described in more detail in the histogram-based gradient boosting section [Histogram-Based Gradient Boosting](http://scikit-learn.org.cn/view/90.html#1.11.5 Histogram-Based Gradient Boosting).The following guide focuses on
GradientBoostingClassifier
andGradientBoostingRegressor
, which may be the preferred choice for small sample sizes, as in this setting, bagging may lead to split points being too close.
-
Import necessary libraries:
from sklearn.ensemble import GradientBoostingClassifier from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split
-
Load data:
# Load the Iris dataset iris = load_iris() X, y = iris.data, iris.target
-
Split the dataset:
# Split into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
-
Create GBDT classifier:
# Create GBDT classifier instance gbdt = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
Tuning parameters is a key step to enhance GBDT performance. Here are some common tuning strategies:
-
n_estimators: Controls the number of weak learners, requiring a balance between model complexity and overfitting risk. -
learning_rate: A smaller learning rate means more weak learners are needed to achieve the same fitting effect. -
max_depth: Controls the depth of decision trees to prevent the model from being too complex. -
subsample: The sample proportion for sampling, less than 1 can reduce overfitting. -
max_features: The maximum number of features to consider when splitting, can be an integer or a percentage.
Train the model:
# Train the model
gbdt.fit(X_train, y_train)
Evaluate the model:
# Predict
y_pred = gbdt.predict(X_test)
# Calculate accuracy
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")