#341Out-of-Fold Target EncodingMediumML System DesignAsked atMeta · Google · Amazon
Out-of-Fold Target Encoding
Background
Naive target encoding leaks the target into the features. Out-of-fold (K-fold) target encoding fixes this: for each row, its encoding is computed only from rows outside its fold. Production tabular pipelines use this as their default category encoder.
Problem statement
Implement oof_target_encode(values, targets, n_folds=5, smoothing=10.0, seed=0). For each row, compute its smoothed target encoding using only rows from the other folds.
Input
values- sequence of hashable category labels.targets- parallel sequence of numeric targets.n_folds-int >= 2.smoothing-float >= 0.seed- RNG seed for fold assignment.
Output
list[float]of length , the per-row OOF encoding.
Examples
Input: 4 rows of category "A" with targets [1,1,1,0], n_folds=2
Output: row in fold 0 uses fold 1 stats; row in fold 1 uses fold 0 stats
Constraints
- Random fold assignment via the seed.
- For categories not present in the out-of-fold partition, use the global prior.
Notes
- Why OOF. Without it, training accuracy is inflated and the model overfits to its own encoding.
Python
Loading...
▶ 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 example
- •Worked sample
- •Reference check: unseen category falls back to prior