Categorical Encoding
Last Updated: July 29, 2026 | By Mihail Sebastian | AI Dictionary
Converts non-numeric categories like color or job title into numbers a machine learning model can compute with, via one-hot, ordinal, or target encoding.
What is Categorical Encoding?
Categorical encoding converts non-numeric categories (values like “Red,” “Berlin,” or “PhD”) into numbers a machine learning model can compute with. The choice of encoding shapes what the model learns: a bad one invents order that isn’t there or leaks the answer into the features.
Types of Categorical Encoding
- One-hot encoding gives each category its own binary column. The default for nominal categories, where no order exists.
- Ordinal (label) encoding assigns each category an integer. Appropriate only when the categories have a real order, such as education levels from high school to PhD.
- Target encoding replaces each category with the average target value observed for it. Compact for high-cardinality variables, but computing it from training labels risks data leakage unless done carefully.
- Hashing trick maps categories into a fixed-length vector with a hash function, trading a few collisions for bounded size.
Choosing an Encoding
Two questions settle most cases. Do the categories have a real order? If yes, ordinal encoding preserves it; if no, one-hot avoids inventing one.
And how many distinct values are there? A handful suits one-hot; thousands (user IDs, ZIP codes) call for target encoding or hashing, because one-hot would explode the column count.
Example of Categorical Encoding
A lender builds a loan-approval model with two categorical fields: education level and marital status.
Education has a natural order, so it becomes ordinal: high school 1, bachelor’s 2, master’s 3, PhD 4. Marital status does not, so it becomes one-hot: separate binary columns for single, married, and divorced.
The model now uses both fields numerically without being misled. Had marital status been encoded 1, 2, 3 instead, the model would treat “divorced” as three times “single,” an ordering no one intended.
Related AI terms: One-Hot Encoding · Hashing Trick · Feature Engineering · Data Leakage · Word Embedding
Did you like the Categorical Encoding gist?
Learn about 250+ need-to-know artificial intelligence terms in the AI Dictionary.
Mihail Sebastian — Writes about AI governance, regulation, and the technology behind them. Placeholder bio — replace with a real credential line. About