Sub topic 18
Choosing between LightGBM and CatBoost
This chapter assumes you can train a model with both libraries, know what early stopping does, and have read the earlier chapters on histogram splits, leaf-wise growth, ordered target statistics, and symmetric trees. The goal here is a decision procedure you can apply to a new dataset.
What actually differs
Both libraries build gradient boosted trees, and on many tabular problems their tuned scores are within noise of each other. The differences are in speed, categorical handling, and how much tuning each one needs.
| Aspect | LightGBM | CatBoost |
|---|---|---|
| Tree shape | Leaf-wise, asymmetric | Symmetric (oblivious) by default |
| Split finding | Histograms, with GOSS and EFB for speed | Histograms, ordered boosting |
| Categoricals | Needs `category` dtype or integer codes. Uses an optimal partition split | Accepts raw strings through `cat_features`. Uses ordered target statistics |
| Default quality | Often needs tuning of `num_leaves` and `min_data_in_leaf` | Strong defaults, often good with little tuning |
| Training speed on CPU | Usually faster, especially on large data | Usually slower per iteration |
| Prediction speed | Fast | Very fast, because symmetric trees evaluate cheaply |
| Overfitting risk | Higher on small data, since leaf-wise trees can grow deep | Lower, since symmetric trees are a strong regularizer |
Decision criteria
- Many high-cardinality categorical columns (user ID, zip code, product SKU). Start with CatBoost. Ordered target statistics encode these without target leakage, and you skip manual encoding.
- Millions of rows and mostly numeric features. Start with LightGBM. Training time is lower, so you can run more experiments.
- Small dataset (a few thousand rows). Start with CatBoost. Its defaults and tree structure resist overfitting. If you use LightGBM, lower
num_leavesand raisemin_data_in_leaf. - Small tuning budget. CatBoost with defaults plus early stopping is a strong baseline. LightGBM needs a few parameters tuned before it competes.
- Latency-sensitive serving. Benchmark both. CatBoost's symmetric trees are often faster at prediction, but model size and your hardware change the result.
A fair comparison
The comparison must use identical folds, the same metric, and early stopping on a validation set that is not your final test set. The script below trains both models on one split of the synthetic dataset from earlier chapters and reports validation AUC and wall-clock time.
import time
import numpy as np
import pandas as pd
import lightgbm as lgb
from catboost import CatBoostClassifier
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
n = 5000
df = pd.DataFrame({
"age": rng.integers(18, 70, n),
"city": rng.choice(["A", "B", "C", "D"], n),
"plan": rng.choice(["free", "basic", "pro"], n),
})
signal = (df["plan"] == "pro") | (df["age"] > 55)
flip = rng.random(n) < 0.1
y = (signal ^ flip).astype(int)
X_train, X_valid, y_train, y_valid = train_test_split(
df, y, test_size=0.2, random_state=0
)
# LightGBM: convert strings to category dtype.
cat_cols = ["city", "plan"]
Xl_train, Xl_valid = X_train.copy(), X_valid.copy()
for col in cat_cols:
cats = pd.CategoricalDtype(sorted(df[col].unique()))
Xl_train[col] = Xl_train[col].astype(cats)
Xl_valid[col] = Xl_valid[col].astype(cats)
start = time.perf_counter()
lgb_model = lgb.LGBMClassifier(n_estimators=1000, learning_rate=0.05, verbose=-1)
lgb_model.fit(
Xl_train, y_train,
eval_set=[(Xl_valid, y_valid)],
eval_metric="auc",
callbacks=[lgb.early_stopping(50, verbose=False)],
)
lgb_time = time.perf_counter() - start
lgb_auc = roc_auc_score(y_valid, lgb_model.predict_proba(Xl_valid)[:, 1])
# CatBoost: raw strings, declared in cat_features.
start = time.perf_counter()
cb_model = CatBoostClassifier(
iterations=1000, learning_rate=0.05, eval_metric="AUC", verbose=0
)
cb_model.fit(
X_train, y_train,
cat_features=cat_cols,
eval_set=(X_valid, y_valid),
early_stopping_rounds=50,
)
cb_time = time.perf_counter() - start
cb_auc = roc_auc_score(y_valid, cb_model.predict_proba(X_valid)[:, 1])
print(f"LightGBM AUC={lgb_auc:.4f} time={lgb_time:.2f}s")
print(f"CatBoost AUC={cb_auc:.4f} time={cb_time:.2f}s")Treat small AUC gaps (for example 0.002) as noise unless repeated across several seeds or folds. Early stopping on the validation set makes its AUC slightly optimistic, so keep a separate test set for the final number.
A practical workflow
Blending the two is a legitimate last step. Their errors differ because the tree shapes and categorical encodings differ, so averaging predictions often gives a small gain over either alone.
Exercise
You have 200,000 rows, 40 numeric columns, and one categorical column with 50,000 distinct user IDs. You have one afternoon to tune. Which library do you try first, and what do you check before committing to it?
Show answer
Try CatBoost first. The high-cardinality user ID column is handled by ordered target statistics without leakage or manual encoding, and its defaults suit a short tuning budget. Before committing, train LightGBM too (with the ID column as a category or dropped) on the same folds with early stopping, compare validation AUC or your chosen metric across several seeds, and check training and prediction time against your requirements.
Exercise
Why is it risky to report the validation score used for early stopping as your final comparison between the two libraries?
Show answer
Early stopping picks the iteration that scored best on that validation set, so the score is slightly optimistic for both models, and by different amounts. A held-out test set that played no part in training or stopping gives an unbiased comparison.