AppsGuidesAI DictionarySmall FixesOn RecordAboutFollow the lab

One-hot encoding

Also called one hot

One-hot encoding turns a category like "chai" or "coffee" into a row of 0s with a single 1, so a model can use it without inventing a false order.

In plain words

Models only work with numbers. But lots of useful information comes as categories: a city, a payment method, a colour, a drink on a café order.

The obvious trick is to number them: chai is 1, coffee is 2, lassi is 3. That quietly tells the model something false. It now thinks lassi is "more" than chai, and that coffee sits halfway between them.

One-hot encoding avoids this. Give every possible category its own column. For each item, put a 1 in its column and 0 everywhere else. Chai becomes 1 0 0 0, coffee becomes 0 1 0 0. Only one spot is "hot", hence the name.

Now every category is equally different from every other, and the model can learn a separate effect for each one. It is one of the most common ways to turn a category into a feature a model can use.

Everyday example

The tick-box order slip

A café gives you an order slip with four boxes: chai, coffee, lassi, juice. You tick exactly one. The slip does not say chai is worth less than juice; it just says which box you ticked. Read the slip as a row of numbers, ticked as 1 and blank as 0, and you have a one-hot code.

Where the example breaks: A café slip has four boxes. Real data can have thousands of categories, and one-hot gives each one a column, so the rows become very long and almost entirely zeros.

Go deeper

How it actually works

Formally, a category from a list of N possibilities becomes a vector of N numbers, exactly one of them 1. Statisticians call the same idea dummy variables.

  1. Build the vocabulary

    First list every category seen in the training data. Rare ones are often lumped into a single catch-all bucket, called out-of-vocabulary, so the model learns one weight for all of them.

  2. One column per category

    Each category gets a position. Google's course uses car colours: with eight colours, every car's colour becomes a list of eight numbers, seven 0s and one 1. With plain index numbers instead, the course notes, a model would treat purple as "six times" orange.

  3. Store it sparsely

    Since almost every entry is 0, software usually just stores the position of the 1. Libraries such as scikit-learn return sparse matrices by default for this reason.

  4. Multi-hot for several at once

    If an item can belong to several categories, such as a film that is both comedy and drama, a 1 goes in each matching column. This variant is called multi-hot encoding.

  5. Where one-hot runs out

    With huge vocabularies, such as every word in a language, one-hot vectors are enormous and say nothing about similarity: "hot dog" is as far from "shawarma" as from "salad". That is why language models replace them with learned embeddings, short lists of numbers that capture meaning.

Also called
One-of-K encoding, or dummy variables in statistics
Shape
N categories become N columns; each row has exactly one 1
Common tool
scikit-learn's OneHotEncoder; can drop one column to avoid duplicated information in plain linear regression
Unseen categories
Either an error, an all-zeros row, or a catch-all "infrequent" column, depending on the setting

Questions people ask

Why is it called one-hot?

Because in each encoded row exactly one position is "hot", set to 1, while all the others are "cold", set to 0. Digital circuit design uses the same term for a group of bits where only one is on at a time.

Why not just number the categories 1, 2, 3?

Numbers imply an order and distances. A model would treat category 3 as bigger than category 1 and category 2 as halfway between, which is nonsense for things like cities or colours. One-hot keeps every category equally separate.

What is the difference between one-hot encoding and label encoding?

Label encoding gives each category a single whole number. One-hot gives each category its own 0 or 1 column. Label encoding suits categories with a real order, such as small, medium and large; one-hot suits categories with no order.

What is the difference between one-hot encoding and embeddings?

One-hot vectors are long, fixed and mostly zeros, and every category is equally distant from every other. Embeddings are short, learned lists of numbers where similar things end up close together. Language models use embeddings for words.

When is one-hot encoding a bad idea?

When a column has thousands of distinct values, such as postcodes or product IDs. The table gets huge and sparse, and the model needs a lot of data to learn each column. Grouping rare values, or using embeddings, usually works better.

Was this clear?

1 is confusing, 5 is crystal clear. One tap, no account needed.

Spotted something wrong, or know a better example? Suggest a change

Further reading