LightGBM › Example

Michael BrenndoerferJuly 12, 20258 min read
Machine Learning from ScratchAll chapters
Part 5 of 9Tree-Based Models
Chapter 45LightGBM › Example
Reading section 5 of 9 · Example · 8 minSection 5 of 9 · 8 min

Book contents

Section 5 of 9 · 8 min read

LightGBM › ExampleOne section · Sections in this chapter5 / 9 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete mathematical example to demonstrate how LightGBM builds trees and makes predictions. We'll use a simple regression problem with a small dataset to make the calculations transparent.

Note: This is a simplified pedagogical example designed to illustrate the core concepts. Real-world applications typically involve much larger datasets and more complex feature interactions.

Dataset

Suppose we have the following training data for predicting house prices based on size and age:

HouseSize (sq ft)Age (years)Price ($)
11005200
215010280
32002350
41208220
518015300
62501450
714012260
81606290

Step 1: Initial Prediction and Gradient Calculation

The initial prediction for all samples is the mean of the target variable:

y^(0)=1n∑i=1nyi=200+280+350+220+300+450+260+2908=280\hat{y}^{(0)} = \frac{1}{n}\sum_{i=1}^{n} y_i = \frac{200 + 280 + 350 + 220 + 300 + 450 + 260 + 290}{8} = 280

For regression with squared error loss, the gradient is the negative residual:

gi=−∂l(yi,y^i(0))∂y^i(0)=−(yi−y^i(0))=y^i(0)−yig_i = -\frac{\partial l(y_i, \hat{y}_i^{(0)})}{\partial \hat{y}_i^{(0)}} = -(y_i - \hat{y}_i^{(0)}) = \hat{y}_i^{(0)} - y_i

Example calculation for House 1:

  • y1=200y_1 = 200 (actual price)
  • y^1(0)=280\hat{y}_1^{(0)} = 280 (initial prediction)
  • g1=y^1(0)−y1=280−200=80g_1 = \hat{y}_1^{(0)} - y_1 = 280 - 200 = 80

Calculating gradients for each sample:

Houseyiy_iy^i(0)\hat{y}_i^{(0)}gi=y^i(0)−yig_i = \hat{y}_i^{(0)} - y_i
120028080
22802800
3350280-70
422028060
5300280-20
6450280-170
726028020
8290280-10

Step 2: Finding the Best Split

LightGBM evaluates potential splits and calculates the gain for each. Let's consider splitting on the 'size' feature. We sort the data by size and consider splits at midpoints between consecutive values:

HouseSizeGradient
110080
412060
714020
21500
8160-10
5180-20
3200-70
6250-170

Potential split points: 110, 130, 145, 155, 170, 190, 225

Let's calculate the gain for splitting at size ≤ 155:

Left child (size ≤ 155): Houses 1, 4, 7, 2, 8

  • ∑i∈ILgi=80+60+20+0+(−10)=150\sum_{i \in I_L} g_i = 80 + 60 + 20 + 0 + (-10) = 150
  • ∣IL∣=5|I_L| = 5

Right child (size > 155): Houses 5, 3, 6

  • ∑i∈IRgi=(−20)+(−70)+(−170)=−260\sum_{i \in I_R} g_i = (-20) + (-70) + (-170) = -260
  • ∣IR∣=3|I_R| = 3

Original node:

  • ∑i∈Igi=80+0+(−70)+60+(−20)+(−170)+20+(−10)=−110\sum_{i \in I} g_i = 80 + 0 + (-70) + 60 + (-20) + (-170) + 20 + (-10) = -110
  • ∣I∣=8|I| = 8

Let's do the calculations step by step:

  • Left child gradient sum: 80+60+20+0+(−10)=15080 + 60 + 20 + 0 + (-10) = 150
  • Right child gradient sum: (−20)+(−70)+(−170)=−260(-20) + (-70) + (-170) = -260
  • Original node gradient sum: 80+0+(−70)+60+(−20)+(−170)+20+(−10)=−11080 + 0 + (-70) + 60 + (-20) + (-170) + 20 + (-10) = -110

Note: For this simplified example, we assume the Hessian (second-order gradient) equals 1 for all samples, which reduces the gain formula to a simpler form. In practice, LightGBM uses the full second-order approximation.

Using the simplified gain formula (assuming hessian = 1 for all samples):

Gain=(∑i∈ILgi)2∣IL∣+(∑i∈IRgi)2∣IR∣−(∑i∈Igi)2∣I∣\text{Gain} = \frac{(\sum_{i \in I_L} g_i)^2}{|I_L|} + \frac{(\sum_{i \in I_R} g_i)^2}{|I_R|} - \frac{(\sum_{i \in I} g_i)^2}{|I|} Gain=15025+(−260)23−(−110)28\text{Gain} = \frac{150^2}{5} + \frac{(-260)^2}{3} - \frac{(-110)^2}{8} Gain=225005+676003−121008\text{Gain} = \frac{22500}{5} + \frac{67600}{3} - \frac{12100}{8} Gain=4500+22533.33−1512.5=25520.83\text{Gain} = 4500 + 22533.33 - 1512.5 = 25520.83

The gain here quantifies how much the split at size ≤ 155 improves the model's objective function (i.e., reduces the loss) compared to not splitting. In gradient boosting, each split is chosen to maximize this gain, which represents the improvement in the model's fit to the data.

  • A higher gain means the split creates child nodes that are more "pure" (i.e., the gradients within each child are more similar), leading to better predictions.
  • The gain calculation uses the sum of gradients in each child and the parent.
  • In this example, a gain of 25,520.83 indicates a substantial reduction in the loss function, making this split highly desirable.

In summary, the gain measures the effectiveness of a split: the larger the gain, the more the split helps the model learn from the data.

Step 3: Leaf-wise Growth Decision

LightGBM would evaluate all possible splits and select the one with the highest gain. In this example, the split at size ≤ 155 shows a substantial gain of 25,520.83, indicating that this split significantly reduces the loss function.

The algorithm would then:

  1. Perform this split, creating two child nodes
  2. Continue the process by finding the next best split among all current leaves
  3. Focus on the leaf that would provide the largest gain reduction

This demonstrates how LightGBM's leaf-wise growth strategy prioritizes the most informative splits, building trees that focus computational effort where it can achieve the greatest loss reduction.

Out[10]:
Visualization
Tree visualization showing split at size ≤ 155 with gradient values in child nodes.
Tree structure showing the split at size ≤ 155, with gradient values for each house in the left and right children. Here's how LightGBM evaluates potential splits and calculates gradient sums for each child node.
Out[11]:
Visualization
Bar chart breaking down gain calculation components showing positive contributions from left child (4500) and right child (22533.33) minus original node value (1512.5).
Visual breakdown of the gain formula components showing how the sum of squared gradients in each child node contributes to the overall gain calculation. The formula is: Gain = (150²/5) + ((-260)²/3) - ((-110)²/8) = 4500 + 22533.33 - 1512.5 = 25520.83. The positive contributions from the left child (4500) and right child (22533.33) are offset by subtracting the original node''s value (-1512.5), resulting in a total gain of 25520.83. This high gain value indicates that this split significantly reduces the loss function, making it a highly desirable split for the decision tree.