LightGBM › Example
Machine Learning from ScratchAll chapters 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:
| House | Size (sq ft) | Age (years) | Price ($) |
|---|---|---|---|
| 1 | 100 | 5 | 200 |
| 2 | 150 | 10 | 280 |
| 3 | 200 | 2 | 350 |
| 4 | 120 | 8 | 220 |
| 5 | 180 | 15 | 300 |
| 6 | 250 | 1 | 450 |
| 7 | 140 | 12 | 260 |
| 8 | 160 | 6 | 290 |
Step 1: Initial Prediction and Gradient Calculation
The initial prediction for all samples is the mean of the target variable:
For regression with squared error loss, the gradient is the negative residual:
Example calculation for House 1:
- (actual price)
- (initial prediction)
Calculating gradients for each sample:
| House | |||
|---|---|---|---|
| 1 | 200 | 280 | 80 |
| 2 | 280 | 280 | 0 |
| 3 | 350 | 280 | -70 |
| 4 | 220 | 280 | 60 |
| 5 | 300 | 280 | -20 |
| 6 | 450 | 280 | -170 |
| 7 | 260 | 280 | 20 |
| 8 | 290 | 280 | -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:
| House | Size | Gradient |
|---|---|---|
| 1 | 100 | 80 |
| 4 | 120 | 60 |
| 7 | 140 | 20 |
| 2 | 150 | 0 |
| 8 | 160 | -10 |
| 5 | 180 | -20 |
| 3 | 200 | -70 |
| 6 | 250 | -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
Right child (size > 155): Houses 5, 3, 6
Original node:
Let's do the calculations step by step:
- Left child gradient sum:
- Right child gradient sum:
- Original node gradient sum:
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):
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:
- Perform this split, creating two child nodes
- Continue the process by finding the next best split among all current leaves
- 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.

