Target Encoding Leakage Lab: Naive vs K-Fold
Interactive 3D lab showing exactly how naive target encoding leaks a row's own label into its feature, inflating training performance, and how out-of-fold (K-fold) encoding fixes it.
Target encoding — replacing a categorical value with the mean target of its category — is one of the most powerful feature-engineering tricks and one of the easiest ways to leak the label into the training set. This lab generates eight synthetic categories, each with its own true mean and a handful of noisy samples, and encodes them two ways: naively (each row's mean includes its own label) and out-of-fold (a K-fold split guarantees no row ever sees itself). Every row is plotted in 3D at (category, encoded value, true target), with a translucent diagonal marking a perfect encoded = target fit, so you can watch the train cloud collapse onto that diagonal under naive encoding — especially with few samples per category — and spread back out to match the honest held-out cloud the moment K-Fold out-of-fold encoding is switched on. Live R² readouts on train and held-out data make the leakage gap a single number instead of an abstract warning.
Watch naive target encoding bake a row's own label into its own feature — inflating training R² — then switch to out-of-fold K-Fold encoding and watch the leakage vanish, live in 3D.
3D · Three.js / WebGL renderer · 60 FPS target · runs fully client-side, no install