Encoding Categorical Features
This lesson explains how categorical values are encoded as numbers, and how the right encoding depends on whether the categories have an order and how many there are.
Learning outcomes
After completing this lesson, you will be able to:
- Choose between label, ordinal and one-hot encoding.
- Encode high-cardinality features with hashing, target encoding or embeddings.
- Apply target encoding without leakage.
Label, ordinal and one-hot encoding
Ordinal encoding maps ordered categories to integers that keep the order, such as small = 1, medium = 2, large = 3. Label encoding maps arbitrary integers to unordered categories; it is fine for tree-based models but misleads linear models into treating red = 1 and blue = 3 as numerically related.
One-hot encoding creates a binary column per category, so no false order is implied. It suits nominal features with a small number of categories. With hundreds or thousands of categories it creates very wide, sparse data.
Choose the encoding
High-cardinality features
One-hot encoding creates a column for every category, which becomes impractical for high-cardinality features such as postal codes or product IDs with thousands of values. Several alternatives exist. Target encoding replaces each category with the mean target value for that category; it must be computed on training folds only, or the label leaks into the feature. Frequency encoding replaces each category with how often it occurs. Feature hashing maps categories into a fixed number of columns, accepting occasional collisions.
Rare categories can be grouped into an "other" category, and neural networks can learn embeddings, dense vectors that place similar categories close together. For sparse interactions such as user and item IDs in recommendation problems, the Factorization Machines algorithm models them efficiently.
The following table summarises the encodings.
| Encoding | Best for | Watch out for |
|---|---|---|
| Ordinal | Ordered categories | Only when order is real |
| Label | Tree-based models | False order for linear models |
| One-hot | Few unordered categories | Width with many categories |
| Target encoding | High cardinality | Leakage unless computed out of fold |
| Hashing | Very high cardinality, streaming | Collisions |
The following diagram shows the encoding chosen for three features, size, colour and zip code, for a logistic regression model and an XGBoost model.

Applying target encoding safely
Target encoding replaces each category with a statistic of the label, usually its mean, which makes it powerful and also easy to misuse. If the mean is computed on the same rows the model then trains on, each row's own label contributes to its feature value, and the model learns from information it will not have in production. Computing the encoding out of fold solves this: the training data is split into k folds, and the value for rows in one fold is computed only from the other folds. The validation and test sets are encoded with statistics from the full training set, never from themselves.
Rare categories need smoothing. A postal code that appears twice, both times with a positive label, would receive a mean of 1.0, which is mostly noise. A smoothed encoding blends the category mean with the global mean, weighted by the number of examples, for example (n x category mean + m x global mean) / (n + m), so categories with few examples stay close to the global average. Categories that appear for the first time at inference receive the global mean.
Every encoding is part of the model. The mapping from category to number, the list of one-hot columns or the hashing configuration must be saved with the model, for example in a scikit-learn pipeline inside the model artefact or as features in Feature Store, and applied identically at inference. Recomputing the mapping separately in the serving code is a common source of training-serving skew, where the same category produces different values in training and production.
The following diagram shows how out-of-fold encoding keeps each row's label out of its own feature.

Target encoding without leakage
Encoding for two models
A dataset has size (S, M, L), color (8 values) and zip_code (30,000 values). It feeds both a logistic regression model and an XGBoost model.
One-hot encoding zip_code would add 30,000 sparse columns, slowing training and risking overfitting. Target encoding computed on training folds keeps one informative column without leaking the label.
How should a category with thousands of values be encoded?
Show answer
With target, frequency or hash encoding. One-hot encoding would create too many columns.
Exam Tips
Remember for the exam
- Unordered categories for linear models: one-hot encoding.
- Ordered categories: ordinal encoding.
- High cardinality: target, frequency or hash encoding.
- Compute target encoding out of fold to avoid leakage.
- Tree models tolerate label encoding.