What is One-Hot Encoding?

A churn dataset has a region column with the values North, South and West. Many models cannot read text, and numbering the regions 1, 2 and 3 would suggest an order and spacing that do not exist. One-hot encoding replaces the column with three yes-or-no columns, one per region.

The scikit-learn guide to preprocessing describes one-hot encoding, also called one-of-K or dummy encoding, as transforming each categorical feature with n possible values into n binary features, with one of them set to 1 and all the others to 0. Its OneHotEncoder can also be told how to handle categories that appear only after training.

Common choices around one-hot encoding:

  • one-hot encoding for categories with no natural order and a manageable number of values
  • ordinal encoding for categories that do have an order, such as small, medium and large
  • grouping rare categories into an “other” value before encoding, so a few unusual records do not each create a column
  • deciding in advance how the model treats a category that was not in the training data

How is one-hot encoding different from label encoding?

  • Label or ordinal encoding replaces each category with a single number, which keeps one column but implies an order.
  • One-hot encoding creates one column per category, which avoids an implied order but adds a column for every value.
  • Both are forms of feature engineering: the right choice depends on whether the categories have an order and on how many there are.

Where does one-hot encoding break in AI work?

One-hot encoding breaks on messy categories. If the same country is written as Korea, KR and Republic of Korea, encoding creates three columns for one place, and the model learns three thin signals instead of one. Fields with thousands of distinct values, such as customer IDs or free text, can turn one column into thousands of mostly empty ones.

In CUBIG’s Syntitan, Ready for Raw Data can align categories that mean the same thing but are written differently, and its diagnosis flags high-cardinality columns that make common patterns hard to learn. Separately from these checks, the encoder should be fitted on training data only, and the categories it learned should be recorded with the data version so that production inputs can be checked against them.

Frequently asked questions

When should you not use one-hot encoding?

When a field has a very large number of distinct values, such as IDs or free text, or when its categories have a meaningful order that a single numeric column would capture better.

How does one-hot encoding handle new categories?

It depends on how the encoder is set up. Some encoders raise an error on an unseen category, while others can be configured to output all zeros or map it to an infrequent category, as scikit-learn's OneHotEncoder allows.

Is one-hot encoding the same as dummy encoding?

The terms are often used interchangeably. Scikit-learn's documentation lists one-of-K, one-hot and dummy encoding as names for the same idea.