samyyc.dev
LOC::Macau, China

..UTC+8

samyyc.dev

-- BPM

Sat May 24 2025

Some Key Points in LightGBM

#115

/

samyyc

/

895 words

/

Updated 559 days ago

/

[中文]

I took part in a machine learning competition and used the LightGBM model. Here I'll break down a few key points for the defense.

First, LightGBM is a GBDT (Gradient Boosting Decision Tree). B stands for Boosting, an ensemble strategy. G means using gradients to calculate the direction for fitting residuals. XGBoost is similar to LightGBM, but LightGBM brings a solid performance boost.

CART Decision Tree

LightGBM is an ensemble model, so let's start with its most basic learner. GBDT models usually use CART (Classification And Regression Tree) as the base learner.

Here's a diagram of a CART decision tree.

Splitting Strategy

The main characteristics of CART are that it uses the Gini coefficient and its gain (in classification) to evaluate improvements (other trees use information entropy and information gain), and each node is a strict binary split: there are no multi-way branches.

The usual CART splitting method is basically the same as other decision trees: first iterate over which feature to split on, then iterate over that feature's values to find the best split point.

The math looks like this:

First define RLR_L and RRR_R, the sets of the two child nodes.

RL(j,s)={(x,y)∈R:xj≤s},RR(j,s)={(x,y)∈R:xj>s}R_L(j,s) = \{(x,y) \in R : x_j \leq s\}, \quad R_R(j,s) = \{(x,y) \in R : x_j > s\}

Next, here's how to choose j∗,s∗j^*, s^*:

j∗,s∗=arg⁡min⁡j,s[∣RL(j,s)∣∣R∣D(RL(j,s))+∣RR(j,s)∣∣R∣D(RR(j,s))]j^*, s^* = \arg\min_{j,s} \left[ \frac{|R_L(j,s)|}{|R|} \text{D}(R_L(j,s)) + \frac{|R_R(j,s)|}{|R|} \text{D}(R_R(j,s)) \right]

j∗,s∗j^*, s^* has the property above: minimizing the weighted DD. The most basic way to choose them is brute-force iteration (though there are optimized methods too).

Here DD is data impurity: classification tasks compute it with the Gini coefficient, regression tasks use MAE.

Boosting

Boosting is an ensemble strategy, alongside bagging and stacking.

Bagging works by randomly sampling multiple subsets from the original data, using multiple base learners to make predictions, and then aggregating the results from all base learners into the final result (for example, majority voting for classification and averaging for regression). A representative algorithm is Random Forest.

Stacking splits the data into k folds, trains a separate base learner on each fold (basically any machine learning model can be used), then trains a meta-learner (usually a simple model like linear regression) to combine the base learners and find the best combination for the final result.

Boosting works by first using one learner to learn the basic data, then using subsequent learners to learn the residual between the previous learner and the true value. This repeats, and finally all learner outputs are weighted and summed to get the final result.

The process is shown below. The residual in the red box is the model's output; for the first classifier, the residual it outputs is just a basic prediction:

Histogram Optimization

Iterating over every feature and its values is very time-consuming when the dataset is large. XGBoost pre-sorts the data to find the best split point quickly, but it still has to scan all the data. LightGBM bins the data, reducing the time complexity from O(ndata×nfeatures)O(n_{data} \times n_{features}) to O(nbins×nfeatures)O(n_{bins} \times n_{features}).

Normally LightGBM puts all data into 255 bins, then iterates over each bin to find the best split point. The benefit is lower time complexity, but the data granularity isn't fine enough, and tiny differences between data points get lost, so the best split point may be buried inside a bin, which can ultimately lead to overfitting.

A visual way to understand it:

Calculating Residuals

As mentioned earlier, in the Boosting strategy each learner needs to compute the residual between the previous learner and the actual data. In practice, though, GBDT models use Newton's approximation to get the Newton step, then multiply it by the learning rate to use as the residual.

The benefit is that Newton's approximation gives the best optimization direction, whereas a residual only describes the difference between two data points on its own. To understand it through the formulas:

For any loss function L(y,y^)L(y, \hat{y}), we want to find the optimal adjustment Δ\Delta:

min⁡ΔL(y,y^+Δ)\min_{\Delta} L(y, \hat{y} + \Delta)\\

Using a second-order Taylor expansion:

L(y,y^+Δ)≈L(y,y^)+g⋅Δ+12h⋅Δ2L(y, \hat{y} + \Delta) \approx L(y, \hat{y}) + g \cdot \Delta + \frac{1}{2}h \cdot \Delta^2

where:

g=∂L∂y^∣y^(first derivative)h=∂2L∂y^2∣y^(second derivative)g = \frac{\partial L}{\partial \hat{y}}\bigg|_{\hat{y}} \quad \text{(first derivative)} \\ h = \frac{\partial^2 L}{\partial \hat{y}^2}\bigg|_{\hat{y}} \quad \text{(second derivative)}

Take the derivative with respect to Δ\Delta and set it to 0:

ddΔ[g⋅Δ+12h⋅Δ2]=g+h⋅Δ=0\frac{d}{d\Delta}\left[g \cdot \Delta + \frac{1}{2}h \cdot \Delta^2\right] = g + h \cdot \Delta = 0

This gives the optimal adjustment:

Δ∗=−gh\boxed{\Delta^* = -\frac{g}{h}}

Here the first derivative (gradient) describes the direction, and the second derivative (Hessian matrix) describes the current slope.

To get an intuitive feel for the final expression for Δ∗\Delta^*:

  • When g>0g > 0, the loss function slopes upward, so we move in the negative direction.
  • When g<0g < 0, the loss function slopes downward, so we move in the positive direction.
  • hh describes the steepness of the loss function at the current point. The larger the slope, the steeper the loss function at that point, and the smaller the value of the fraction, meaning a small step can bring a large drop, and vice versa.