Sub topic 10
CatBoost: what it does differently
This chapter assumes you know how gradient boosted trees work, how to hold out a validation set, and how LightGBM trains with early stopping. CatBoost is a gradient boosting library from Yandex. It fits the same kind of tree ensemble, but it changes three parts of the algorithm. Later chapters cover each change in detail. This chapter gives you the overview.
The three design choices
Standard boosting has two weak spots. Categorical columns need manual encoding, and the gradients used to fit each tree are computed on the same rows the model already trained on. CatBoost addresses both and also fixes the tree shape.
| Design choice | What it replaces | Main benefit |
|---|---|---|
| Ordered target statistics | One-hot or label encoding done by you | Handles high-cardinality categories without target leakage |
| Ordered boosting | Gradients computed on rows the model has already seen | Reduces prediction shift, which matters most on small data |
| Symmetric (oblivious) trees | Free-form trees | Fast prediction and a built-in regularizer |
Categorical features as a first-class input
You pass raw string columns and list their names in cat_features. CatBoost replaces each category with a statistic of the target, such as the mean label for that category. It computes the statistic only from rows that come earlier in a random permutation. This stops a row's own label from leaking into its feature value. The encoding chapter explains the details.
Ordered boosting
In ordinary boosting, the residual for a row comes from a model that was trained on that row. That makes the residuals slightly optimistic, a bias called prediction shift. Ordered boosting estimates each row's residual with a model that has not seen that row. The effect is largest on small datasets. CatBoost applies this mode mainly on small data, and you can choose it with the boosting_type parameter.
Symmetric trees
A symmetric tree uses the same split condition for every node at a given depth. A tree of depth 6 is therefore a table of 64 leaves, indexed by six yes/no answers. Prediction becomes a fast lookup. The shape also limits how much each tree can fit, which acts as regularization. The cost is flexibility, because the tree cannot isolate one narrow region with a deep branch the way LightGBM's leaf-wise trees can.
A first look in code
The API mirrors what you already know. The main difference is the Pool, which bundles features, labels and the categorical column names.
import numpy as np
import pandas as pd
from catboost import CatBoostClassifier, Pool
rng = np.random.default_rng(0)
n = 2000
df = pd.DataFrame({
"age": rng.integers(18, 70, n),
"plan": rng.choice(["free", "basic", "pro"], n),
})
y = ((df["plan"] == "pro") | (df["age"] > 55)).astype(int)
pool = Pool(df, y, cat_features=["plan"])
model = CatBoostClassifier(iterations=100, depth=6, verbose=0)
model.fit(pool)
print(model.predict(df.head()))Common errors
- Passing string columns without
cat_featuresraises an error, because CatBoost then expects numbers. - Label-encoding categories yourself and then listing them in
cat_featuresis fine only if they are integers. Floats are rejected. - Expecting leaf-wise behavior.
depthsets the exact tree depth, so there is nonum_leavesto tune.
Exercise
Why does a symmetric tree of depth 6 have exactly 64 leaves, and why is prediction fast?
Show answer
Every node at the same depth uses the same split, so six splits give 2^6 = 64 leaves. Each row answers six yes/no questions, which form a binary index into a table of leaf values, so prediction is one lookup.