← All AI/ML articles

CatBoost, visualised.

Categories like user IDs or zip codes are hard: replace each one by its average label and the model can end up peeking at the answer. CatBoost encodes every row using only the rows that came before it, then grows perfectly symmetric trees. Watch the leak, then watch it get reduced.

1. Encode categories2. Don’t peek3. Grow symmetric trees
Each column is one category (A–L); height is a numeric feature. Click inside a column to add a row of the selected class · Shift-click (or right-click) a row to remove it · Hollow dots are held out for validation · White ring = the model gets it wrong.
Train accuracy0
Validation accuracy0
Points0
Data: how much do the categories really matter?
Behind the scenes · 1

Turning a category into a number, without cheating

A category becomes a smoothed target statistic: TS = (Σy + a·p) / (n + a), where Σy is the sum of labels counted, n is how many rows were counted, p is the overall average label (the prior) and a is the prior weight that pulls rare categories toward p. The naive version counts every row of the category, including the row being encoded, so a rare category’s number contains its own label. The ordered version shuffles the rows and encodes each one using only the rows before it (n = preceding rows of that category). CatBoost supports several categorical-processing schemes; this page shows just this one.


label = cyanlabel = pinkring = the category chosen on the right
Encoding one category, row by row

If the dots in the top strip split cleanly by colour, the encoded value is carrying the row’s own label: a tree can split on it, look brilliant on training data, and then do worse on new rows whose encoding never contained their own label. In the ordered strip the colours stay mixed, because a row’s encoding never saw its own answer. This is a risk, not a guarantee: naive encoding does not make every model worse on every dataset, and it hurts most for rare categories.


Simplification: this page uses a single random order, which is the easiest way to see the idea. The real CatBoost algorithm uses several permutations and applies the procedure during training, so treat the row-by-row table as an illustration rather than the exact implementation.

Behind the scenes · 2

Symmetric trees: one question per level

A normal tree can ask a different question at every node. CatBoost’s oblivious trees ask the same question at every node of a level, so a tree of depth d is just d questions and up to 2d leaves (two to the power d; some leaves may end up with no training rows). This is a strong regulariser, and it makes prediction very fast. The map shows what one tree contributes across the categories and the numeric feature.


leaf pushes toward cyanleaf pushes toward pink
The tree’s whole definition
Behind the scenes · 3

What the leak can cost you

Both boosted models below use the same trees settings, learning rate and data. Only the encoding differs. Accuracy is always measured with the encoding a new row would receive. Naive target encoding can leak a training row’s own label into its feature value. That can make training performance look artificially good and encourage overfitting, especially for rare categories. Ordered statistics reduce this risk by using only preceding observations in a permutation. The log-loss gap is the useful signal: two models can have the same rounded accuracy while one is more overconfident on its training rows.


train log-lossvalidation log-lossdashed = naive encoding
Experiment

Break it on purpose

Memorise the IDs

Categories carry no real signal here. Use the naive encoding, go deep, boost 40 rounds, and watch it memorise.

Ordered to the rescue

Same data, same trees, ordered encoding. The leak is reduced and the validation gap usually shrinks.

When categories matter

Make the categories truly informative. Now the encoding earns its keep and both modes can do well.

Check yourself

Three quick questions

Key takeaways

  • Categories can be turned into numbers with a smoothed target statistic, for example (sum of labels + a·prior) / (count + a). CatBoost offers several schemes; this is the one shown here.
  • Target leakage: if a row’s own label is inside its encoding, the model can learn a shortcut that does not exist for new data. The risk is largest for rare categories.
  • Ordered statistics reduce this by encoding each row using only the rows before it in a random permutation. The real algorithm uses several permutations; this page uses one for clarity.
  • Oblivious trees use the same split at every node of a level: d splits give up to 2d leaves. Simple, fast and regularised.
  • Ordered boosting is the companion idea: also compute the gradients for each row from a model that never trained on that row. It is not simulated on this page.
← Back to all articles