Machine LearningCategorical Data & Feature Engineering

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
Green

One-hot encoding creates one binary feature for each category:

is_red
is_blue
is_green

A row with:

Color = Blue

could be represented as:

is_red   = 0
is_blue  = 1
is_green = 0

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

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

#what is one hot encoding#one hot encoding machine learning#one hot encoding categorical variables#categorical data encoding#one hot encoding example#machine-learning#one-hot-encoding#categorical-data#categorical-encoding#feature-engineering

Learning Sequence

Machine Learning Curriculum

28% Completed
Up NextMachine Learning

When should one-hot encoding be used?

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.