What is one-hot encoding?
One-hot encoding is a technique for converting categorical values into separate binary features that a machine learning model can use.
It is commonly used for nominal categories, where the categories do not have a natural order.
Simple example
Suppose a feature contains three colors:
Red
Blue
GreenOne-hot encoding creates one binary feature for each category:
is_red
is_blue
is_greenA row with:
Color = Bluecould be represented as:
is_red = 0
is_blue = 1
is_green = 0Only the feature corresponding to the observed category is set to 1; the others are set to 0.
Why one-hot encoding is useful
If nominal categories were encoded as:
Red = 1
Blue = 2
Green = 3some models might incorrectly interpret those numbers as having an order or numerical distance.
One-hot encoding avoids introducing that artificial ranking.
When one-hot encoding works well
It is especially useful when:
- categories have no natural order
- the number of unique categories is manageable
- the model expects numerical input
Main limitation: more features
A categorical feature with many unique values can create a very large number of one-hot columns.
For example, a city feature with 10,000 unique cities could create thousands of additional features.
This is why other encoding strategies may be more appropriate for high-cardinality categorical data.
Unseen categories
Production or test data may contain a category that was not present during training.
The encoder should be configured to handle these unseen categories safely rather than failing unexpectedly.
Fit the encoder on training data only
The set of known categories should be learned from the training data and then applied consistently to validation and test data.
In short, one-hot encoding represents each category as its own binary feature, allowing nominal categorical data to be used by machine learning models without creating a false numerical ordering between categories.
Learning Sequence
Machine Learning Curriculum
Explore Related Questions
Deepen your understanding across related Machine Learning topics.