Machine LearningCategorical Data & Feature Engineering

What is ordinal encoding?

Ordinal encoding converts ordered categorical values into numerical values while preserving their natural ranking.

It is mainly used for ordinal data, where the categories have a meaningful order.

Simple example

Suppose a feature contains:

Low
Medium
High

Ordinal encoding could map them as:

Low = 1
Medium = 2
High = 3

The numerical values preserve the original order:

Low < Medium < High

Why ordinal encoding is useful

Some machine learning algorithms require numerical input.

Ordinal encoding converts ordered categories into numbers without losing the ranking information.

This makes it different from one-hot encoding, which creates separate binary features for each category.

The numerical distance may not be meaningful

Ordinal encoding preserves order, but it does not prove that the gaps between categories are equal.

For example:

Low = 1
Medium = 2
High = 3

does not necessarily mean the difference between Low and Medium is exactly the same as the difference between Medium and High.

Some models may nevertheless treat those values as ordinary numbers, so this assumption should be considered carefully.

Use a meaningful order

The mapping should reflect the real domain order.

For example:

Beginner = 1
Intermediate = 2
Advanced = 3

is meaningful because the categories have a clear progression.

Assigning an arbitrary order to nominal categories such as cities or colors would usually be inappropriate.

Handle unknown categories

Validation, test, or production data may contain categories that were not seen during training.

The preprocessing pipeline should define how those unknown categories are represented.

Fit the encoding from training data

Any learned category mapping should be established using the training data and applied consistently to later datasets.

In short, ordinal encoding converts ordered categories into numerical values that preserve their ranking, but the resulting numerical distances should not automatically be interpreted as equal or quantitatively meaningful.

#what is ordinal encoding#ordinal encoding machine learning#encode ordinal categorical data#ordinal feature encoding#ordinal encoding example#machine-learning#ordinal-encoding#categorical-data#categorical-encoding#feature-engineering

Learning Sequence

Machine Learning Curriculum

29% Completed
Up NextMachine Learning

What is label encoding and when is it appropriate?

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.