AI Glossary / AI and machine learning / Beginner

One-hot encoding

Definition

One-hot encoding turns a category into a set of yes-no columns: 'city' becomes one column per city, with a 1 in the item's own city and 0 everywhere else, so models can read categories as numbers.

What it means

Most models accept only numbers, and categories are not numbers: you cannot average Paris and Cairo. One-hot encoding solves it by replacing the category with a row of switches, one per possible value.

Each switch is a clean numeric fact, is-this-Paris, zero or one, and the model can learn a weight for each category separately, which is exactly the flexibility categories need.

The method's price is width: a field with a thousand categories becomes a thousand columns, most of them zero, and high-cardinality fields can drown a model in switches.

The usual remedies are grouping rare categories into an 'other' bucket, dropping one column to avoid redundancy in linear models, or switching to learned embeddings when categories are many and rich.

For a business, one-hot encoding is where spreadsheet categories become model inputs, and the decisions that matter, how many columns, what to do with the rare ones, are data decisions, not algorithm decisions.

The scheme predates machine learning: dummy variables in classical statistics are the same switches, renamed, and the logic of one column per category is identical.

How it works

List the field's categories and decide the bucket for rare values, since a category seen twice teaches nothing.

Create one binary column per kept category, marking each item's own category with 1 and the rest with 0.

Drop one column for linear models to avoid perfect redundancy, since the dropped category is implied by the others.

Handle unseen categories at prediction time deliberately: map them to 'other' or all zeros, never crash or guess.

Record the encoding, the full category list and its order, because a model's columns mean nothing without it.

Decide the unknown-category policy before launch: every production system eventually meets a category its training never saw.

Three practical examples

  • A churn model with region encoded as twelve one-hot columns, letting each region carry its own weight in the score.
  • An 'other' bucket holding the bottom two per cent of product categories, keeping the feature table to two hundred columns.
  • A serving failure traced to a new city appearing in production that the training-time encoder had never seen.

Think of it this way

Think of a form where you tick one box from a list: the form does not record the word, it records which box is ticked. One-hot encoding is that ticked-box grid, one row per item, one column per box.

Worked example

the unseen city

Suppose a delivery model encodes city as one-hot over the forty cities it trained on. The figures are illustrative.

Launch day in city forty-one: every request fails encoding, or worse, encodes as all zeros and gets a prediction built for nowhere.

The fix is policy, not code: an 'other' column absorbs unseen cities, an alert fires when it activates, and retraining with the new city is scheduled.

The incident generalizes: an encoder's category list is part of the model, and a new category is a model change, not a data detail.

Illustrative case study

A fictional retailer one-hot encoded eight thousand product categories raw, producing a table wider than it was long, and the model memorized noise across thousands of sparse columns.

Performance on new products was near random, because each rare category had been learned as an isolated fact with two examples.

The invented lesson: every one-hot column is a claim of evidence. Categories without data belong in the 'other' bucket, not in the model.

Common misconceptions and mistakes

  • It is the only way to encode categories. It is the simplest; embeddings, target encoding and others exist for wide or rich category spaces.
  • More columns always add information. Each column adds a claim that needs data behind it; sparse columns add noise, not knowledge.
  • Unseen categories are a serving detail. They are a design decision: the encoder's vocabulary is part of the model and changes with it.

Business use

Cap category counts with an 'other' bucket sized by evidence, not by tidiness.

Version the encoder with the model, since the category list and column order define what the model reads.

Alert on unseen categories in production and treat them as a trigger for review.

Consider embeddings when categories are numerous and meaningful, where one column per category stops being honest.

Review the category vocabulary quarterly, since new values and dying ones both silently age the encoding.

People also ask

What is one-hot encoding?

Turning each category into its own yes-no column, 1 for the item's category and 0 for the rest.

Why is it needed?

Models consume numbers, and one-hot gives each category a clean numeric identity without inventing false orderings.

What is its main weakness?

Width: many categories mean many sparse columns, and rare categories become noise columns.

What happens with a new category?

By design, not accident: it maps to 'other' or all zeros, with an alert, since the model never learned it.

Check your understanding

Why is the encoding's category list part of the model itself?

Answer: Because the model's weights attach to specific columns, and the columns exist only for the categories the encoder knew: a new category changes what the model can read, making the vocabulary a component of the model rather than a data detail.

Related terms

  • One-hot vector
  • Categorical data
  • Embedding
  • Feature engineering
  • Encoding (for Features)
  • encoding
  • high cardinality

Learn next

Be Productive Using AI builds the practical data judgment behind choices like these: https://aiforbusinessandlife.com/academy/be-productive-using-ai. [C1]

More in AI and machine learning

  • Numerical data - Numerical data is data that lives on a number line: quantities with magnitude and order, like revenue, temperature, age or count, where arithmetic means...
  • Oblique condition - An oblique condition is a decision-tree split on a combination of features at once, a slanted line through the data, rather than the usual single-feature...
  • On-Demand Features - On-demand features are computed at the moment a prediction is requested, from the freshest available data, rather than being pre-computed in a batch and...
  • One-shot learning - One-shot learning is learning to recognize a category from a single example: show the system one photo of a new product, one recording of a new voice, and...
  • One-vs-one - One-vs-one is a scheme for multi-class classification that trains one binary classifier for every pair of classes: ten classes mean forty-five pairwise...
  • One-vs-Rest - One-vs-Rest is a scheme for multi-class classification that trains one binary model per class, each answering a single question: is this item a member of my...

Sources

[S24] scikit-learn documentation, Glossary of Common Terms and API Elements; [S12] Aalto University, Dictionary of Machine Learning. Full URLs are in the source ledger. Verified 11 October 2026.

Previous: One-for-one checking Next: One-hot vector

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer