Categorical Encoding Cheat Sheet
Every encoding makes an assumption about the structure of a variable — here is when to use which one.
TL;DR: The best encoding is not the most sophisticated one — it is the one whose assumptions match your variable's actual structure and your model's learning mechanism.
The Problem
After years of working with tabular models, one lesson has stayed consistent: every categorical encoding makes an assumption about the structure of a variable. Choose an encoding that does not match the variable, and you hide useful signal or create leakage that looks like signal. The question is not "which encoding is most advanced?" — it is "what does this category actually mean, and what model am I using?"
The Approach
A practical starting point for encoding decisions:
Few unordered levels
One-hot encoding is usually the clean baseline.
A real ranking exists
Ordinal encoding can preserve that order.
Many levels with stable target signal
Target encoding can help, but only with smoothing and out-of-fold training.
Binary classification with scorecard-style interpretability needs
WoE remains useful.
Ordered factors in a linear model
Contrast coding, such as Helmert or polynomial contrasts, can make specific comparisons explicit.
Small or noisy groups
Use stronger shrinkage rather than trusting each group's raw rate.
The model matters too. A tree-based model may handle ordinal integers very differently from a linear model. And a target encoder that looks excellent in a random validation split can fail badly once leakage is removed.
Outcome
The point is not to use every encoding method — it is to make your assumption explicit. Are categories simply different? Is there a meaningful order? Does target behaviour carry stable information? Is the signal strong enough to survive a new dataset? Ten minutes spent answering those questions per variable can save days of debugging a model that memorizes instead of generalizes.
Key Takeaway
Design insight: The best encoding is not the most sophisticated one — it is the one whose assumptions match your variable's actual structure and your model's learning mechanism.
Related
One-Hot, Target Encoding, or Embeddings? →Preprocessing should have one source of truth → Polars Lazy Execution: More Than a Faster pandas → Cyclical Encoding: Stop One-Hot Encoding Months →Preprocessing Is Vocabulary Design for Noisy NLP Text →Unpack a Categorical Variable Into Behavioural Mechanisms →isnull() Is a Feature →Encoding Is a Modeling Decision →Necessary Conditions vs Drivers →Don't Ask Your Model to Learn What a Formula Already Knows →Adversarial Validation →SHAP Waterfall Local Explanation →LightGBM vs XGBoost in 2026 →Financial Inclusion →Sendy Logistics →Focal Loss for Imbalanced Classification →AI Can Review Feature Code. It Cannot Approve It →When the Label Does Not Exist: Define the Behaviour First →LLM Semantic Features: Propose, Then Earn Their Place →
CatBoost Ordered Target Encoding: Controlling the Leak →FAQ
What is the key takeaway from "Categorical Encoding Cheat Sheet"?
The best encoding is not the most sophisticated one — it is the one whose assumptions match your variable's actual structure and your model's learning mechanism.
Who wrote this and what is it about?
This was written by Mahmoud Trigui, Senior Data Scientist. Practical guide to categorical encoding methods for tabular ML: one-hot, ordinal, target, WoE, and contrast coding — when to use each and common pitfalls.