Smoothed Target (Mean) Encoding
Background
Categorical features with many levels (user ID, zip code, product SKU) cannot be one-hot-encoded without exploding the feature space. Smoothed target encoding replaces each category with the average of the target restricted to that category, blended toward a global prior so rare categories don't overfit. It is the technique that dominates Kaggle leaderboards in tabular / CTR contexts.
Problem statement
Implement smoothed_target_encode(values, targets, smoothing=10.0, prior=None) returning a dict mapping each category to its smoothed encoding:
where is the number of samples in category , is the mean target in that category, is the global target mean (the prior), and is the smoothing strength. A high pulls rare categories toward the global mean; a low trusts each category's own mean.
Input
values- sequence of category labels (hashable), length .targets- sequence of numeric targets, same length.smoothing-float >= 0, prior weight (default ).prior- optionalfloat; ifNone, use the global target mean.
Output
dict[label -> float]of encodings, one entry per unique value invalues.
Examples
Example 1 - balanced classes, no smoothing pull needed
Input: values = ["A","A","B","B"], targets = [1, 1, 0, 0], smoothing = 0
Output: {"A": 1.0, "B": 0.0}
Example 2 - rare category pulled to the global prior
Input: values = ["A","A","A","A","A","A","A","A","A","B"],
targets = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
smoothing = 10
Output: {"A": ~0.95, "B": ~0.818}
Explanation: global prior = . "A": . "B": (heavily pulled to the prior).
Constraints
- The prior defaults to the overall target mean (
sum(targets) / n). Pass an explicitpriorto fix it across slices (e.g. for leave-one-out variants). smoothing = 0reduces to raw mean target per category (no shrinkage).- Return a dict with one entry per unique value (no row-aligned output).
Notes
- Leakage. The naive version computes the encoding on the training set that the encoder is then applied to — this directly leaks the target. Production setups use out-of-fold (K-fold) target encoding so the encoding for each row is computed from rows outside its fold. That is the second hidden problem in this set.
- Why smoothing matters. A rare category with 1 sample has a mean of either 0 or 1 — pure noise. The Bayesian-flavoured shrinkage to the prior keeps that noise from dominating the model.
- Multi-class / regression. Same formula; "target mean" is the class-conditional mean for binary problems, the literal mean for regression, or the per-class probabilities one column at a time for multi-class.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference: smoothing zero raw means
- •Sample: rare category to prior
- •Example: explicit prior override