Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 40 ยท LightGBM and CatBoost

CatBoost and Ordered Boosting

Learn what makes CatBoost different from XGBoost and LightGBM: ordered target statistics for categories, ordered boosting, symmetric trees and its API, with runnable stand-ins for the ideas.

CatBoost, from Yandex, is the third big gradient boosting library. Its name comes from "categorical boosting", and it earned its reputation by giving strong results on tables full of categories with very little tuning. It shares the boosting idea of this course but changes three things: how categories are encoded, how the boosting loop avoids a subtle bias, and what shape the trees have.

What runs here. CatBoost is not available in the in-browser Python runner of this site, so the CatBoost code blocks in this lesson are read-only. The ideas are demonstrated with small NumPy stand-ins that do run, and each is labelled as a stand-in, not CatBoost itself.

Idea 1: ordered target statistics for categories

A popular way to encode a category is target encoding: replace each category with the average target value of its rows. If you compute that average using all rows, including the row you are encoding, the feature leaks the label, and the model overfits to it. CatBoost avoids this by processing rows in a random order and encoding each row using only the rows that came before it.

For a row with category c, the encoded value is roughly:

TEXT
(sum of targets of earlier rows with category c + a * prior) / (count of earlier rows with category c + a)

The prior (often the global mean) and the smoothing weight a keep rare categories from producing wild values. The first rows of each category have no history and fall back to the prior.

This runnable stand-in compares naive (leaky) target encoding with the ordered version, using a feature that carries no signal. A leak shows up as an apparent correlation with the target.

Python Stand-in: leaky vs ordered target encoding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The target is random, so any good encoding should correlate with it by about zero. The naive encoding shows a clearly positive correlation (a pure leak, which a model would happily exploit and then fail on new data), while the ordered encoding stays close to zero.

Idea 2: ordered boosting

A second, related bias exists inside boosting itself. Each tree is fit to gradients computed from a model that has already seen those very rows, so the residuals are slightly "too good" on the training data (a prediction shift). CatBoost's ordered boosting trains models on prefixes of a random permutation, so that the gradient for a row comes from a model that has not seen that row. It costs more computation, so CatBoost offers both boosting_type="Ordered" and "Plain" and chooses a default from the size of the dataset; you can set it yourself.

Idea 3: symmetric (oblivious) trees

In a CatBoost tree every node at the same depth uses the same split condition. A depth-d tree is therefore just d yes/no questions, and a row's leaf is found by reading the answers as bits of an index into a table of 2^d leaf values. That makes predictions very fast and acts as a strong regularizer.

Python Stand-in: a symmetric tree is a lookup table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because the three questions are the same for every row, the tree needs only three comparisons per prediction, whatever its leaf values. LightGBM and XGBoost instead let each node choose its own split, which is more flexible and can fit complicated interactions with fewer total leaves.

What the CatBoost API looks like

The library follows the scikit-learn pattern. This is the shape of a typical program (read-only here):

Python
from catboost import CatBoostClassifier, Pool

train_pool = Pool(X_train, y_train, cat_features=["city", "device"])
valid_pool = Pool(X_valid, y_valid, cat_features=["city", "device"])

model = CatBoostClassifier(
    iterations=2000,
    learning_rate=0.05,
    depth=6,
    eval_metric="AUC",
    random_seed=0,
    early_stopping_rounds=100,
    verbose=200,
)
model.fit(train_pool, eval_set=valid_pool)
proba = model.predict_proba(X_valid)[:, 1]

Key points:

  • Categorical columns are declared by name in cat_features; you pass raw strings, not codes.
  • Pool is CatBoost's data container, like DMatrix or Dataset.
  • The key size parameters are depth (tree depth, default 6) and iterations, with learning_rate set automatically if you leave it out.
  • l2_leaf_reg is the L2 penalty. border_count plays the role of max_bin.
  • Defaults are strong, so CatBoost often does well with very little tuning, which is why people call it "the safe first choice" on categorical-heavy data.
  • GPU training (task_type="GPU") is mature and fast.

Strengths and trade-offs

Aspect CatBoost LightGBM / XGBoost
Raw categorical strings yes, with ordered target statistics LightGBM yes (subset splits), XGBoost yes (2.x)
Defaults very good out of the box need more tuning
Tree shape symmetric free-form
Training speed on big tables moderate LightGBM is usually faster
Prediction latency very low low
Overfitting control strong built in manual
flowchart LR
  A["Rows in random order"] --> B["Encode category from earlier rows only"]
  B --> C["Fit symmetric trees on ordered gradients"]
  C --> D["Sum of trees"]

Ordering shows up twice: in the categorical statistics and in the boosting loop.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is naive target encoding dangerous?

    A category mean computed with the row itself is correlated with that row's label, so the model overfits to the leak.
  2. 2. How does CatBoost encode a row's category value?

    Using only earlier rows removes the self-leak.
  3. 3. What is special about a symmetric tree?

    A depth-d tree is d questions read as a bit index into 2^d leaf values.
  4. 4. Which statement about running CatBoost on this site is true?

    CatBoost is not available in the in-browser runner, so the code is shown without being executed.

Summary

  • CatBoost encodes categories with ordered target statistics to avoid label leakage.
  • Ordered boosting reduces the prediction-shift bias in the boosting loop.
  • Its trees are symmetric, which makes them fast to evaluate and strongly regularized.
  • You declare categorical columns by name and use Pool; defaults are strong.
  • It cannot run on this site, so its code is read-only and the ideas are demonstrated with labelled stand-ins.

Next: Choosing a Booster