TL;DR
A target statistic replaces a category, such as a city or a user ID, with one number: the average of the target over past rows that had the same category, pulled gently toward an overall average when there are few rows. This lets a model use a column with thousands of values without a column for each value. Used carelessly it leaks the answer into the training data, so the number must be built only from other rows.
Builds on: Conditional Expectation, One-Hot Encoding · Leads to: Ordered Boosting, CatBoost
Where it's used
Click prediction in advertising. An ad network may see millions of distinct publisher IDs. One-hot encoding would need a column for each, and almost all of them would be 0 in almost every row. Target encoding replaces the ID with one number: the click rate that publisher showed in the past.
Gradient-boosted trees on tables. Gradient Boosting models work well on tables with category columns such as occupation, product or region. The CatBoost library, described in a 2018 paper, was built around this problem, and its key trick addresses the leak that the naive version of the encoding causes.
Insurance and credit scoring. A risk model may include an occupation code. A rare occupation with three past customers must not receive an extreme score just because those three happened to have no claims. Smoothing toward the overall average handles this.
How it works
Six past rows record a city (A or B) and whether the visitor clicked (1) or not (0). The rows are in the order they arrived.
| Row | City | Click |
|---|---|---|
| 1 | A | 1 |
| 2 | B | 0 |
| 3 | A | 0 |
| 4 | A | 1 |
| 5 | B | 1 |
| 6 | A | 1 |
- Start with the column to encode. Two values, A and B. The goal is to replace each letter with a number that says how often that city's rows clicked in the past.
- Count clicks per city. City A has rows 1, 3, 4 and 6 with clicks 1, 0, 1, 1: 3 clicks in 4 rows. City B has rows 2 and 5 with clicks 0, 1: 1 click in 2 rows. Check: 3 + 1 = 4, and the table has four 1s.
- Use only rows that came earlier. Walk down the rows. Each row gets a number built from earlier rows of its own city, plus a prior of 0.5 counted as one imaginary row. Row 1 has no earlier A rows, so its number is the prior: (0 + 0.5) / (0 + 1) = 0.5. Row 2 is the first B row: 0.5. Row 3 has one earlier A row, which clicked: (1 + 0.5) / (1 + 1) = 0.75.
- Keep going down the list. Row 4 has earlier A rows 1 and 3, with one click: (1 + 0.5) / (2 + 1) = 0.5. Row 5 has earlier B row 2, with no click: (0 + 0.5) / (1 + 1) = 0.25. Row 6 has earlier A rows 1, 3 and 4, with two clicks: (2 + 0.5) / (3 + 1) = 0.625.
- Hand the model the numbers. The column now reads 0.5, 0.5, 0.75, 0.5, 0.25, 0.625 instead of A and B. A tree can split these numbers as it splits any other number.
- Score a new row. At prediction time there is no order to respect, so use all six training rows. City A has 3 clicks in 4 rows, so a new A visitor gets (3 + 0.5) / (4 + 1) = 0.7.
flowchart LR A["Category value of a row"] --> B["Earlier rows with the same value"] B --> C["Clicks plus prior, divided by rows plus prior weight"] C --> D["One number fed to the model"]
The math
For a row with category value and target , the target statistic is Here is the set of rows allowed to contribute, is how many there are, is the prior value (for example the overall average of the target), and is the prior weight, the number of imaginary rows the prior counts as.
Step 2 in symbols
For city A, the targets of the four rows are 1, 0, 1, 1, so and . These two totals are all the next steps need from the city's history.
Step 3 in symbols
Ordering means that contains only the earlier rows with the same value: With and , row 6 has earlier A rows 1, 3 and 4, with targets 1, 0, 1. So .
Step 6 in symbols
At prediction time is every training row with the same value, . As the count grows, the prior fades and the value approaches the average target among rows with that category, which is the conditional expectation from the conditional expectation lesson.
The prior does this shrinking. For a city whose observed click rate is 0.8, with prior and , the value after visits is . The curve below shows it starting at the prior and moving toward the observed rate as visits accumulate.
- smoothed value
- observed rate 0.8
Worked examples
A category "red" appears in two rows, with clicks 1 then 0. Use prior and .
- Row 1 has no earlier red rows. Its value is .
- Row 2 has one earlier red row, which clicked. Its value is .
- Interpretation: row 1 has no history, so it gets the prior. Row 2's number comes only from row 1. Its own click of 0 does not enter its value.
A category "tea" appears in four rows, with clicks 0, 1, 1, 0 in that order. Use and .
- Row 1: no earlier rows. Value .
- Row 2: one earlier row with 0 clicks. Value .
- Row 3: two earlier rows with 0 and 1 clicks, so 1 click. Value .
- Row 4: three earlier rows with 0, 1 and 1 clicks, so 2 clicks. Value .
- Interpretation: without smoothing row 4 would get . The prior pulls it down to 0.625, a small step, because three rows is not many.
A shop has seen 120 visits from zip code 94103, and 30 of them clicked. The shop uses prior with weight .
- Clicks: 30. Visits: 120.
- The prior adds imaginary clicks over 10 imaginary visits.
- Numerator: . Denominator: .
- Value: . The raw rate was , and the prior nudges it slightly toward 0.2, because 120 visits is plenty of evidence.
Try it: zip code 10001 has 3 visits and 0 clicks, with the same prior and weight. What is its value? Answer: . A raw rate of 0 has become 0.154, because three visits is too few to be certain that the true rate is zero.
Common mistakes and tradeoffs
It is tempting to encode each category with the average target over all training rows, including the row itself. In fact this leaks the answer. Without smoothing, a category with one row gets exactly that row's own target: a row with click 1 gets the value 1. A model learns the rule "value 1 means click", which is perfect on the training data and useless on new data, because new rows have no such shortcut. This is a kind of overfitting.
It is tempting to think that leave-one-out, which excludes the row itself, fixes this. In fact it still leaks. In the six-row example, city A has sum 3 and count 4, so a row's leave-one-out value is . A row with click 1 gets , and a row with click 0 gets 1.0. Within the city, a lower value always means a click, and a tree can find that split. Only computing from strictly earlier rows removes the link to the row's own target.
It is tempting to skip smoothing when a category has many rows. In fact the rarest categories are the ones with the most extreme values. A category with 3 visits and 0 clicks gives a raw rate of 0 and looks certain, which it is not. The prior keeps such values near the overall average.
When to use it: when a column has many distinct values, each with enough rows to estimate a rate, and the target is known in training. When not to use it: when the column has a handful of values, where one-hot encoding is simpler and easier to read, or when there is no target to average. Alternatives include learned embeddings for very large vocabularies, and out-of-fold target encoding, where each row is encoded from other folds of the data; that approach is a form of Cross-Fitting, checked with cross-validation folds, and is easier to explain than ordering.
History
Statisticians long shrank noisy group averages toward an overall mean. Herbert Robbins described this kind of reasoning in the 1950s under the name empirical Bayes, and the prior-weighted average is its simplest form. In 2001 Daniele Micci-Barreca proposed using this kind of encoding for categorical inputs with many values in classification and prediction problems. In 2018 the CatBoost paper introduced the ordered version used here, which computes each row's value only from earlier rows, so that boosting never sees the row's own answer.
Recap
- A target statistic turns a category into the smoothed average of the target over rows with that category.
- Using the row's own target leaks the answer, and leave-one-out still leaks, so each row is encoded from earlier rows or from other folds.
- The prior weight keeps rare categories from getting extreme values.
- At prediction time the encoding uses every training row, and a category never seen in training gets the prior itself.
- Read next: Gradient Boosting, which uses these numbers in its trees, and Ordered Boosting, which applies the ordering idea inside boosting. The CatBoost paper puts both together.