How do you encode categorical variables?
Categorical variables are encoded by converting categories into a numerical representation that a machine learning model can use.
The right encoding depends on whether the categories have an order, how many unique values exist, and which algorithm is being used.
Start by understanding the category type
First determine whether the feature is:
- Nominal: categories have no natural order, such as
Red,Blue,Green - Ordinal: categories have a meaningful order, such as
Low,Medium,High
This distinction helps determine which encoding method is appropriate.
One-hot encoding
For nominal features with a manageable number of categories, one-hot encoding is commonly used.
Each category becomes its own binary feature.
For example:
Color = Red, Blue, Greencan become separate features such as:
is_red
is_blue
is_greenOne-hot encoding is covered separately in the following articles.
Ordinal encoding
When categories have a meaningful order, they can be mapped to ordered numbers.
For example:
Low = 1
Medium = 2
High = 3This preserves the ranking information.
Integer or label-style encoding
Sometimes each category is assigned a unique integer.
For example:
Delhi = 0
Mumbai = 1
Bengaluru = 2For nominal data, these numbers are only identifiers and should not be treated as a meaningful ranking unless the model can handle them appropriately.
Target-based encoding
For high-cardinality categorical features, methods such as target encoding may replace each category with a statistic derived from the target.
This can be useful, but it introduces a serious risk of data leakage if it is not implemented carefully.
Target encoding is covered separately later.
Some models can handle categories directly
Certain algorithms and libraries can work with categorical features without manually applying standard one-hot encoding.
In those cases, preserving the original categorical information may be preferable.
Handle unseen categories
Production data may contain categories that were not present during training.
The preprocessing pipeline should define how these unseen categories are handled rather than failing unexpectedly.
Fit encoders using training data only
Any learned encoding information should be created from the training data and then applied consistently to validation and test data.
This is especially important for encodings that use frequencies or target values.
In short, categorical variables are encoded by choosing a numerical representation that matches the category type, cardinality, and model. Common approaches include one-hot encoding, ordinal encoding, integer encoding, and target-based encoding.
Learning Sequence
Machine Learning Curriculum
Explore Related Questions
Deepen your understanding across related Machine Learning topics.