Sub topic 11
CatBoost: ordered boosting and prediction shift
This chapter assumes you know how gradient boosting fits each new tree to the gradients of the loss, and that you can train a CatBoost model with Pool. It explains one design choice in CatBoost, ordered boosting, and shows when to switch it on.
The problem: prediction shift
In standard boosting, each round computes gradients for every training row using the current model. That model was already fitted on those same rows, so it has seen their labels. The gradients are therefore biased. They look smaller and less noisy than they will on unseen rows.
The next tree learns from these biased gradients. The distribution it was trained on differs from the one it meets at prediction time. CatBoost's authors call this prediction shift. It is a form of target leakage that works through the gradients. The effect is largest on small or noisy datasets.
The idea: ordered boosting
Ordered boosting removes the leak by changing how gradients are computed.
- Draw a random permutation of the training rows.
- For row i, compute its gradient with a model trained only on the rows that come before i in the permutation.
- Use these gradients to choose the structure of the next tree.
No row's gradient depends on its own label.
Training a separate model for every prefix would be far too slow. CatBoost keeps a limited set of supporting models for prefixes of the permutation and reuses them. This is why ordered mode costs more time and memory than plain mode.
Using it
The boosting_type parameter selects the mode: "Plain" or "Ordered". The default depends on dataset size and hardware, so set it explicitly when you compare the two.
import numpy as np
import pandas as pd
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 = 1000
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.15
y = (signal ^ flip).astype(int)
X_train, X_valid, y_train, y_valid = train_test_split(
df, y, test_size=0.3, random_state=0
)
for mode in ["Plain", "Ordered"]:
model = CatBoostClassifier(
iterations=300,
learning_rate=0.05,
boosting_type=mode,
cat_features=["city", "plan"],
random_seed=0,
verbose=0,
)
model.fit(X_train, y_train)
auc = roc_auc_score(y_valid, model.predict_proba(X_valid)[:, 1])
print(mode, round(auc, 4))The two scores will often be close. On a dataset this small the difference is mostly noise, so repeat the comparison over several seeds or use cross-validation before drawing conclusions.
Common mistakes
- Treating ordered mode as always better. It is slower, and the gain on large data is usually negligible.
- Comparing the two modes on a single split. Differences of a few thousandths of AUC are within seed variation.
- Confusing ordered boosting with ordered target statistics. Both use a permutation to avoid leakage. The first protects gradients and the second protects categorical encodings.
Exercise
Why does computing a row's gradient from a model that was trained on that row cause prediction shift, and how does ordered boosting avoid it?
Show answer
The model has already seen the row's label, so the gradient is biased and does not match the gradients the model sees on unseen data. Ordered boosting computes each row's gradient with a model trained only on the rows earlier in a random permutation, so the row's own label never influences its gradient.