Machine LearningCategorical Data & Feature Engineering

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, Green

can become separate features such as:

is_red
is_blue
is_green

One-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 = 3

This preserves the ranking information.

Integer or label-style encoding

Sometimes each category is assigned a unique integer.

For example:

Delhi = 0
Mumbai = 1
Bengaluru = 2

For 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.

#how to encode categorical variables#categorical encoding machine learning#encode categorical features#categorical variable encoding methods#machine learning categorical data preprocessing#machine-learning#categorical-data#categorical-encoding#feature-engineering#data-preprocessing

Learning Sequence

Machine Learning Curriculum

28% Completed
Up NextMachine Learning

What is one-hot encoding?

Explore Related Questions

Deepen your understanding across related Machine Learning topics.

Machinoai Logo
Machinoai

Your edge in the AI era. Deep dives into machine learning concepts, paper explainers, verified industry jobs, and developer tools.

© 2026 Machinoai. All rights reserved.