Sub topic 1
LightGBM and CatBoost for Tabular Data
Prerequisites
This chapter assumes Python and pandas basics, supervised learning, decision trees, train/validation splits, and common metrics such as RMSE, log loss, and AUC.
Tabular Data and Validation
Tabular data stores observations in rows and features in columns. A label is the value the model must predict. Split the data into training and validation sets. Fit only on training data, then measure performance on validation data. The validation set estimates performance on unseen examples.
Keep related rows together when splitting. For time-dependent data, train on earlier records and validate on later records. For classification, use a stratified split so each set has similar class proportions.
How Gradient Boosted Trees Work
A gradient boosted decision tree (GBDT) is an ensemble built one tree at a time. The first tree makes a rough prediction. Each later tree fits the gradients of the loss, which indicate how predictions should change to reduce error. The model adds a small, scaled contribution from each tree.
This sequential process can model nonlinear relationships and feature interactions without extensive feature engineering.
flowchart TD
A[Training data] --> B[Initial prediction]
B --> C[Compute loss gradients]
C --> D[Fit a small decision tree]
D --> E[Add scaled tree prediction]
E --> CLightGBM: Histogram-Based Split Finding
LightGBM groups continuous feature values into discrete bins called a histogram. It evaluates splits between bins instead of every distinct value. This reduces memory use and speeds up training, especially on large datasets. Binning introduces approximation, but usually preserves useful split information.
LightGBM: Leaf-Wise Tree Growth
Many tree algorithms grow level by level. LightGBM grows leaf wise: it splits the leaf that promises the largest loss reduction. This can improve accuracy with the same number of leaves, but it can also create deep, overfit branches.
LightGBM: GOSS and Exclusive Feature Bundling
Gradient-based One-Side Sampling (GOSS) keeps more rows with large gradients because they carry more learning information. It samples more aggressively from rows with small gradients.
Exclusive Feature Bundling (EFB) combines sparse features that rarely contain nonzero values at the same time. This reduces the effective feature count.
> callout note > GOSS and EFB are implementation optimizations. Validate their effect on your own data rather than assuming they always improve results.
The LightGBM Parameters That Matter Most
| Parameter | Main effect |
|---|---|
| `num_leaves` | Controls tree complexity |
| `min_child_samples` | Requires more samples in each leaf |
| `learning_rate` | Shrinks each tree's contribution |
| `feature_fraction` | Samples columns for each tree |
Use fewer leaves or more samples per leaf to reduce overfitting. A lower learning rate usually needs more boosting rounds. Use early stopping with a validation set to stop when the metric no longer improves.
import lightgbm as lgb
model = lgb.LGBMRegressor(
n_estimators=2000,
learning_rate=0.05,
num_leaves=31,
min_child_samples=20,
feature_fraction=0.8,
)
model.fit(
X_train,
y_train,
eval_set=[(X_valid, y_valid)],
eval_metric="rmse",
callbacks=[lgb.early_stopping(50)],
)Exercise
Why can leaf-wise growth overfit more easily than level-wise growth?
Show answer
It repeatedly chooses the leaf with the largest immediate loss reduction, which can produce a very deep branch that models noise.