Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Don’t let a row peek at its own answer
A quick way to turn a category such as “sensor serial number” into a number is to replace each value with the average outcome of its rows. That is very informative, but dangerous: if a serial number appears only once, its “average outcome” is that row’s label. On training data the feature then looks perfect; on new data it is useless. CatBoost fixes this by processing rows in a random order and encoding each row using only the rows before it, so no row ever sees its own label.
Engineering data is full of high-cardinality categories: part numbers, sensor IDs, test stations, firmware versions, operators. CatBoost handles them with no manual encoding and strong defaults, and its symmetric trees give very fast, predictable inference, useful on embedded systems.
- 1. A target statistic replaces a category by…
- 2. Greedy target statistics leak because…
- 3. In the demo, a noise category with many levels looks predictive…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.Target statistics by hand
Smoothed statistic: TS = (Σy + a·p)/(count + a), with prior p (the global mean) and strength a.
Example: category “S17” has 4 rows with labels 1, 1, 0, 1; p = 0.5 and a = 1.
-
Greedy (all rows): (3 + 0.5)/(4 + 1) = 0.7 for every S17 row, each including its own label.
-
Ordered, processing the rows in the order shown:
- row 1: (0 + 0.5)/(0 + 1) = 0.5 (prior only);
- row 2: (1 + 0.5)/(1 + 1) = 0.75;
- row 3: (2 + 0.5)/(2 + 1) = 0.833;
- row 4: (2 + 0.5)/(3 + 1) = 0.625.
No row uses its own label.
-
Unique categories: a category seen once gets greedy TS (y + 0.5)/2, which is 0.75 if y = 1 and 0.25 if y = 0, a perfect leak. Ordered TS gives it the prior 0.5.
Oblivious tree. At depth 2 with splits (x₀ > 0.5) and (x₁ > 0.5), every row’s leaf is the 2-bit code (x₀ > 0.5, x₁ > 0.5), with values 0–3. Prediction is a lookup in a 4-entry table.
The steps in plain English:
- Draw a random permutation of the training rows (CatBoost uses several).
- Encode each categorical value by the smoothed target mean of earlier rows only. At test time, use all training rows.
- Compute gradients with ordered boosting: each row’s residual comes from a model trained only on earlier rows. In practice CatBoost approximates this with a few supporting models.
- Grow oblivious trees (one split per level), add them with shrinkage, and repeat.
- 1. Greedy TS for 3 positives in 4 rows, p = 0.5, a = 1?
- 2. Ordered TS for the first row of a category?
- 3. Leaves of a depth-6 oblivious tree?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.Prediction shift and the ordered principle
Target leakage in target statistics. If x̂ᵢ = TS of xᵢ computed with yᵢ included, then E[x̂ᵢ | yᵢ] differs between training and test rows. On training rows the feature carries information about yᵢ that it cannot carry at test time: a conditional shift. The fewer rows per category, the worse.
Fixes.
- Holdout or K-fold TS encode each row using other folds. This is leak-free, but uses less data per statistic.
- Ordered TS (CatBoost) encode row σ(k) with rows σ(1..k−1) of a random permutation σ. Every row’s statistic uses only “past” rows, like an online learner. Early rows get high-variance statistics, so CatBoost averages over several permutations.
Prediction shift in boosting (the same problem for gradients). Standard boosting computes row i’s residual from a model trained on row i, so training residuals are biased toward 0 compared with test residuals, and the trees fit that bias. Ordered boosting maintains models M_k trained on the first k rows of σ and uses M_(i−1) to compute row i’s gradient. CatBoost implements this efficiently with logarithmically many supporting models.
Oblivious trees. The same split is used at every node of a level.
- Prediction is a table lookup indexed by d bits: fast and cache-friendly.
- They are strongly regularised (fewer distinct splits) and balanced.
- They work well with CatBoost’s many weak trees.
Feature combinations. CatBoost greedily combines categorical features (for example station × firmware) and computes target statistics for the combinations.
from catboost import CatBoostClassifier
m = CatBoostClassifier(iterations=2000, learning_rate=0.05, depth=6, eval_metric="Logloss", od_type="Iter", od_wait=100)
m.fit(X_tr, y_tr, cat_features=["station", "firmware", "serial"], eval_set=(X_va, y_va))- 1. Prediction shift in boosting arises because…
- 2. Oblivious trees use…
- 3. CatBoost averages over several permutations because…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Theory
Why leakage hurts boosting specifically. Boosting greedily picks the feature that best reduces training loss. A leaky encoded feature reduces training loss the most, especially for high-cardinality noise categories, so it gets chosen often, and its splits fail on test data. Exercise 2: with greedy target statistics, 23% of all splits used the pure-noise category, and test log loss was worse than ignoring the categories entirely (0.864 against 0.565).
Ordered versus K-fold. Both remove the leak. K-fold uses (K − 1)/K of the data for every statistic, and ordered uses a varying amount (more for later rows). In exercise 2 they tied (0.529 against 0.534). Ordered statistics combine naturally with ordered boosting and online settings.
Smoothing prior. The prior a·p shrinks statistics of rare categories toward the global mean. This is empirical-Bayes shrinkage, equivalent to a Beta prior on each category’s rate. Larger a means more shrinkage for small categories.
Interview questions.
- What’s wrong with mean target encoding done naively? It leaks each row’s label into its own feature, so high-cardinality categories look predictive on training data and overfit. Use out-of-fold or ordered encodings with smoothing.
- Why are oblivious trees useful? They are fast table lookups at inference, strongly regularised, and balanced. The cost is less flexibility per tree, compensated by more trees.
- 1. With greedy TS in exercise 2, the noise category got about what share of splits?
- 2. The smoothing prior is equivalent to…
- 3. K-fold and ordered target statistics in exercise 2…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Encoding categories for boosting
History.
- Micci-Barreca (2001) introduced smoothed target (“impact”) encoding.
- Yandex developed MatrixNet, then CatBoost (released 2017; NeurIPS 2018, Prokhorenkova, Gusev, Vorobev, Dorogush and Gulin).
- CatBoost introduced ordered boosting and ordered target statistics.
- Oblivious trees go back to Kohavi and Li’s oblivious decision graphs (1995).
- CatBoost added GPU training and text and embedding features.
Exercise 1.
- On a unique-ID feature, greedy target statistics correlated 1.000 with the label (total leakage).
- Ordered statistics gave every unseen ID just the prior 0.473.
- On a real 20-level category, ordered statistics still tracked the true rates (r = 0.88).
- A depth-2 oblivious tree recovered both steps as a 4-entry table.
Exercise 2. Setup: two numeric features, an informative 100-level category A and a pure-noise 500-level category B, n = 2000, logistic boosting with depth-3 trees, 150 rounds, 2 repetitions.
| Encoding of A and B | test log loss | splits on noise category B |
|---|---|---|
| greedy target statistics | 0.864 | 23% |
| 5-fold out-of-fold statistics | 0.529 | 14% |
| ordered statistics (CatBoost) | 0.534 | 11% |
| drop both categories | 0.565 | (none) |
- Greedy encoding was far worse than dropping the categories. The leaked labels in B and A made boosting overfit badly.
- Leak-free encodings captured category A’s real signal, about 6% better log loss than dropping, and they tied with each other.
- The noise category still drew some splits under leak-free encodings (11–14%). Its statistics are noisy for 500 levels, even if unbiased. Stronger smoothing or minimum counts reduce this.
CatBoost in AI, robotics and aerospace
- Manufacturing and test data with part numbers, stations and lots as categories.
- Fleet telemetry keyed by unit, firmware and mission phase.
- Fast embedded inference: oblivious trees compile to bit-index lookups.
- Robust defaults when there is little time for feature engineering.
References
- L. Prokhorenkova, G. Gusev, A. Vorobev, A. V. Dorogush, A. Gulin (2018). “CatBoost: unbiased boosting with categorical features.” NeurIPS.
- A. V. Dorogush, V. Ershov, A. Gulin (2018). “CatBoost: gradient boosting with categorical features support.” arXiv:1810.11363.
- D. Micci-Barreca (2001). “A preprocessing scheme for high-cardinality categorical attributes in classification and prediction problems.” SIGKDD Explorations 3(1).
- R. Kohavi, C.-H. Li (1995). “Oblivious decision trees, graphs, and top-down pruning.” IJCAI.
- P. Cerda, G. Varoquaux (2022). “Encoding high-cardinality string categorical variables.” IEEE TKDE 34(3).