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
HighOrdinal encoding could map them as:
Low = 1
Medium = 2
High = 3The numerical values preserve the original order:
Low < Medium < HighWhy 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 = 3does 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 = 3is 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.
Learning Sequence
Machine Learning Curriculum
What is label encoding and when is it appropriate?
Explore Related Questions
Deepen your understanding across related Machine Learning topics.