Sub topic 4
LightGBM: leaf-wise tree growth
This chapter assumes you know how gradient boosted trees add one tree per round, and that LightGBM finds splits on histogram bins. It covers how LightGBM chooses which node of a tree to split next, and which parameters follow from that choice.
Two ways to grow a tree
A level-wise algorithm splits every node at the current depth before moving down. The tree stays balanced, but it spends splits on nodes where a split barely helps.
LightGBM grows trees leaf-wise, also called best-first. At each step it looks at every current leaf, computes the best split for each, and splits only the leaf whose split reduces the loss the most. Growth stops when the tree reaches num_leaves leaves or when no split meets the constraints.
With the same number of leaves, a leaf-wise tree usually reaches a lower training loss than a level-wise tree. The cost is that it can build deep, lopsided branches that fit a few noisy rows.
Parameters that control the shape
Because growth is driven by leaf count, num_leaves is the main complexity setting. Depth is only a side effect.
| Parameter | Default | Role |
|---|---|---|
| `num_leaves` | 31 | Maximum leaves per tree. Main complexity control. |
| `max_depth` | -1 (no limit) | Hard depth cap. Use it as a safety limit. |
| `min_data_in_leaf` | 20 | Minimum rows in a leaf. In the scikit-learn API, the alias is `min_child_samples`. |
| `min_gain_to_split` | 0.0 | Minimum loss reduction needed to split. |
Seeing the effect
The script below trains three models that differ only in num_leaves and prints the final validation RMSE for each.
import lightgbm as lgb
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.2, random_state=0
)
for num_leaves in (7, 31, 127):
model = lgb.LGBMRegressor(
n_estimators=300,
learning_rate=0.05,
num_leaves=num_leaves,
min_child_samples=20,
verbose=-1,
)
model.fit(
X_train,
y_train,
eval_set=[(X_valid, y_valid)],
eval_metric="rmse",
)
rmse = model.evals_result_["valid_0"]["rmse"][-1]
print(num_leaves, round(rmse, 4))Expect validation RMSE to improve as num_leaves grows from 7 to 31. At 127 the gain shrinks or reverses on small data. Exact numbers depend on your library version.
Common errors
- Copying depth rules from other libraries. In a level-wise library,
max_depth=6means up to 64 leaves. In LightGBM,num_leaves=64withmax_depth=-1can produce a tree far deeper than 6. - Setting
num_leavesabove 2^max_depth. The depth cap then limits the tree, and the extra leaves are never used. Keepnum_leavesbelow 2^max_depth. - Raising
num_leaveson small data without raisingmin_data_in_leaf. Leaves end up holding a handful of rows, and the model overfits. - Tuning
num_leavesbefore fixing the learning rate and early stopping. Complexity comparisons are only fair when each model stops at its own best iteration.
Exercise
A LightGBM model uses numleaves=255 and maxdepth=6. Does the model use all 255 leaves? Why or why not?
Show answer
No. A tree of depth 6 has at most 2^6 = 64 leaves, so maxdepth caps the tree well below 255. Either lower numleaves to 64 or less, or raise max_depth.