Three libraries, one algorithm, different engineering
Everything in this chapter sits on top of how gradient boosting machines fit each tree. The algorithm does not change. What changes is how the split search is implemented, how categorical columns are handled, and how much of your afternoon the fit consumes. The short answer to xgboost vs lightgbm vs catboost: LightGBM for speed on wide numeric data, CatBoost when you have high-cardinality categorical columns, XGBoost when you want the most predictable behaviour and the deepest documentation. Accuracy differences are usually small and sometimes they are not, as the benchmark below shows.
I ran all four engines on the same invented dataset: 40,000 advances from a trade-credit lender, predicting default inside twelve months. Five numeric columns, plus trade with ten levels and broker with 300. The broker column is the interesting one, because 300 levels is exactly where the libraries start behaving differently from each other.
The dataset
import time
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
rng = np.random.default_rng(85)
n = 40000
brokers = [f"BRK{i:03d}" for i in range(300)]
trades = ["plumbing", "roofing", "joinery", "electrical", "groundworks",
"plastering", "glazing", "flooring", "painting", "landscaping"]
broker = rng.choice(brokers, n)
trade = rng.choice(trades, n)
broker_risk = dict(zip(brokers, rng.normal(0, 0.9, len(brokers))))
trade_risk = dict(zip(trades, rng.normal(0, 0.5, len(trades))))
advance = rng.lognormal(9.4, 0.55, n).round(0)
term_weeks = rng.integers(4, 53, n)
months_trading = rng.integers(1, 240, n)
late_filings = rng.poisson(0.5, n)
credit_use = rng.beta(2, 3, n).round(3)
logit = (-3.4
+ 0.0000022 * advance
+ 0.012 * term_weeks
- 0.004 * months_trading
+ 0.35 * late_filings
+ 1.4 * credit_use
+ np.array([broker_risk[b] for b in broker])
+ np.array([trade_risk[t] for t in trade]))
y = (rng.uniform(size=n) < 1 / (1 + np.exp(-logit))).astype(int)
df = pd.DataFrame({"advance": advance, "term_weeks": term_weeks,
"months_trading": months_trading, "late_filings": late_filings,
"credit_use": credit_use, "broker": broker, "trade": trade})
for c in ("broker", "trade"):
df[c] = df[c].astype("category")
X_tr, X_te, y_tr, y_te = train_test_split(df, y, test_size=0.25,
random_state=0, stratify=y)
print("default rate", round(y.mean(), 4), "| broker levels", df.broker.nunique())
# default rate 0.1073 | broker levels 300
Note how the broker effect enters: a per-broker risk offset drawn once and applied to every row from that broker. That is how real portfolio data behaves, and it is the pattern that rewards a library able to learn a value per category rather than a split position.
XGBoost vs LightGBM vs CatBoost on the same table
from sklearn.ensemble import HistGradientBoostingClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
res = []
# scikit-learn refuses native categoricals above 255 levels, so broker goes in as codes
Xh_tr = X_tr.assign(broker=X_tr.broker.cat.codes)
Xh_te = X_te.assign(broker=X_te.broker.cat.codes)
t = time.perf_counter()
h = HistGradientBoostingClassifier(max_iter=400, learning_rate=0.08, max_depth=6,
categorical_features=["trade"],
random_state=0).fit(Xh_tr, y_tr)
res.append(("HistGradientBoosting", time.perf_counter() - t,
roc_auc_score(y_te, h.predict_proba(Xh_te)[:, 1])))
t = time.perf_counter()
xgb = XGBClassifier(n_estimators=400, learning_rate=0.08, max_depth=6,
enable_categorical=True, tree_method="hist",
random_state=0, n_jobs=-1).fit(X_tr, y_tr)
res.append(("XGBoost", time.perf_counter() - t,
roc_auc_score(y_te, xgb.predict_proba(X_te)[:, 1])))
t = time.perf_counter()
lgb = LGBMClassifier(n_estimators=400, learning_rate=0.08, max_depth=6,
num_leaves=31, random_state=0, n_jobs=-1,
verbose=-1).fit(X_tr, y_tr)
res.append(("LightGBM", time.perf_counter() - t,
roc_auc_score(y_te, lgb.predict_proba(X_te)[:, 1])))
t = time.perf_counter()
cat = CatBoostClassifier(iterations=400, learning_rate=0.08, depth=6,
cat_features=["broker", "trade"], verbose=0,
random_seed=0).fit(X_tr, y_tr)
res.append(("CatBoost", time.perf_counter() - t,
roc_auc_score(y_te, cat.predict_proba(X_te)[:, 1])))
for name, secs, auc in res:
print(f"{name:22s} {secs:6.2f}s AUC {auc:.4f}")
# HistGradientBoosting 0.28s AUC 0.6955
# XGBoost 1.35s AUC 0.6983
# LightGBM 0.69s AUC 0.7140
# CatBoost 5.95s AUC 0.7517
| Library | Fit time | Test ROC AUC | Categoricals |
|---|---|---|---|
| HistGradientBoostingClassifier | 0.28 s | 0.6955 | native, 255 levels maximum |
| XGBoost 3.4.1 | 1.35 s | 0.6983 | native with enable_categorical=True |
| LightGBM 4.7.0 | 0.69 s | 0.7140 | native from pandas category dtype |
| CatBoost 1.2.10 | 5.95 s | 0.7517 | native, ordered target statistics |
Fit times are from one four-core sandbox run and moved by up to ten per cent on a repeat; the AUC column is deterministic.
CatBoost wins by 0.038 AUC and pays roughly eight times LightGBM’s fit time for it. On a portfolio this size that trade is obviously worth taking. The gap is not a general fact about CatBoost; it is a fact about 300 categories whose effect is a per-category offset. CatBoost’s ordered target statistics replace a category with a running estimate of the target computed from earlier rows only, which is close to the ideal encoding for exactly this structure. Everyone else has to find split positions among 300 levels.
Also note what scikit-learn did: it refused the column outright. HistGradientBoostingClassifier caps native categorical features at 255 levels and raises ValueError above that. Passing the integer codes instead, as above, treats broker ordering as meaningful, which it is not, and that is part of why it sits bottom of the table.
What happens if you drop the categoricals
cols = ["advance", "term_weeks", "months_trading", "late_filings", "credit_use"]
lgb2 = LGBMClassifier(n_estimators=400, learning_rate=0.08, max_depth=6,
random_state=0, n_jobs=-1, verbose=-1).fit(X_tr[cols], y_tr)
print("categoricals dropped, AUC",
round(roc_auc_score(y_te, lgb2.predict_proba(X_te[cols])[:, 1]), 4))
# categoricals dropped, AUC 0.5801
0.5801 against 0.7140. Over half the usable signal in this dataset lives in two text columns. Choosing a library is a smaller decision than deciding how those columns get represented, which is the subject of encoding categorical variables.
The small data surprise
CatBoost is usually recommended for small datasets with categoricals. On 3,000 rows of this data it lost.
small = df.iloc[:3000]
Xs_tr, Xs_te, ys_tr, ys_te = train_test_split(small, y[:3000], test_size=0.25,
random_state=0, stratify=y[:3000])
for name, m in [("LightGBM", LGBMClassifier(n_estimators=400, learning_rate=0.08,
max_depth=6, random_state=0,
n_jobs=-1, verbose=-1)),
("CatBoost", CatBoostClassifier(iterations=400, learning_rate=0.08,
depth=6, cat_features=["broker", "trade"],
verbose=0, random_seed=0))]:
m.fit(Xs_tr, ys_tr)
print(f"{name:10s} 3,000 rows AUC {roc_auc_score(ys_te, m.predict_proba(Xs_te)[:, 1]):.4f}")
# LightGBM 3,000 rows AUC 0.6929
# CatBoost 3,000 rows AUC 0.6496
With 2,250 training rows spread over 300 brokers there are roughly seven rows per broker, and a target statistic computed from seven observations is mostly noise. The advantage that made CatBoost win at 40,000 rows is the thing that hurt it at 3,000. Report results like this when you find them rather than quietly rerunning with a kinder split.
How they differ under the bonnet
Three design choices account for most of the behaviour you will observe.
Tree growth. XGBoost grows level by level: every leaf at depth 3 is split before any leaf goes to depth 4. LightGBM grows leaf-wise, always splitting whichever leaf promises the largest loss reduction, which yields deeper lopsided trees for the same leaf count. That is why LightGBM is fast and why it overfits small datasets unless you cap num_leaves or raise min_child_samples. The LightGBM features documentation sets out the leaf-wise argument in detail.
Categorical handling. LightGBM sorts categories by gradient statistics and looks for a split in that order. CatBoost computes ordered target statistics with a permutation scheme designed to avoid target leakage. XGBoost added partition-based categorical splits more recently and needs enable_categorical=True plus a pandas category dtype.
Defaults. CatBoost’s defaults are the most forgiving: 1,000 iterations, depth 6, and a learning rate it picks from the data size. LightGBM’s defaults overfit small tables. XGBoost sits between them. The XGBoost tree method guide explains why hist is now the sensible default there.
My actual rule: start with LightGBM because iteration speed matters more than the last 0.003 of AUC while you are still deciding what the features should be, then test CatBoost once the categorical columns are settled. Benchmark on your data. Timings here came from one sandbox run with four cores, and the ordering of the fast three could flip on different hardware. Judge the outcome with ROC AUC on a held-out split, never on training score.
Frequently Asked Questions
Which of the three is most accurate?
None of them, consistently. Across published tabular benchmarks the three finish within noise of each other once tuned, and the winner is decided by your data’s shape. On the lender data above CatBoost led by 0.038 AUC purely because of one 300-level column; strip that column out and the gap largely disappears.
Do I need these libraries if scikit-learn has HistGradientBoosting?
Often not. HistGradientBoostingClassifier is fast, well documented and fits the pipeline API without adapters. You outgrow it when you have categorical columns above 255 levels, want GPU training, or need features like monotonic constraints and custom objectives that the dedicated libraries expose more fully.
Is CatBoost only for categorical data?
No, but that is where it earns its name and its slowest fits. On purely numeric tables it performs comparably to the others and takes longer, so there is little reason to prefer it.
Key Takeaways
- The three libraries run the same algorithm; they differ in split search, categorical handling and defaults, so expect speed differences to be larger and more reliable than accuracy differences.
- On 40,000 rows with a 300-level broker column CatBoost reached 0.7517 AUC against LightGBM’s 0.7140, at roughly eight times the fit time.
- The same CatBoost advantage reversed on 3,000 rows, where target statistics computed from about seven rows per category became noise.
- Dropping the two categorical columns cost 0.134 AUC, far more than any choice between libraries, so spend your effort on representation first.
- Use LightGBM while you are iterating on features, then benchmark CatBoost once the categorical columns are fixed, and keep
HistGradientBoostingClassifieras the zero-dependency fallback.