One-Hot Encoding

One-Hot Encoding

One-hot encoding is a technique that converts categories like "dog," "cat," or "bird" into number sequences so that an AI model can perform calculations with them. Each category is assigned its own position in the sequence, which is marked with a 1 – all other positions remain 0.

AI models work exclusively with numbers. Words, colors, or categories like “Monday” or “dog” must therefore first be translated into numbers. One-hot encoding is one of the simplest methods for this. You start by defining a list of all possible categories. For each category, you then create a number sequence in which exactly one position is set to 1 – all others remain 0. The 1 marks the matching category, while the zeros stand for all the others. With three categories – for example dog, cat, bird – it looks like this: dog becomes [1, 0, 0], cat becomes [0, 1, 0], and bird becomes [0, 0, 1].

Why categories aren’t simply numbered

The obvious idea would be to simply number the categories: dog = 1, cat = 2, bird = 3. That sounds simple, but it has a decisive flaw. The model would treat the numbers as actual quantities. It would calculate as if bird were worth three times as much as dog, or as if cat lay exactly between the two. But that’s not true – the three animals have no such ranking relative to one another.

One-hot encoding completely avoids this problem. Each category gets its own position in the number sequence, and all distances between the categories are equally large. The model cannot read a false ranking into the data, because none exists. This is crucial for anything that works with unordered categories: colors, countries, product types, weekdays.

How categories become number sequences

The first step is to define the vocabulary – that is, all the categories that can occur. Suppose a model is meant to assess weather conditions: sunny, rain, snow, cloudy. The vocabulary has four entries, so each one-hot sequence is four positions long. “Rain” then becomes [0, 1, 0, 0], because rain is in second position in the vocabulary.

The procedure is deterministic: the same category always results in the same sequence, and two different categories always result in different sequences. There is no ambiguity. Calculations that the model performs internally are directly possible on these sequences – that was the actual goal of the conversion.

A practical problem arises with large vocabularies. If a text model knows 50,000 different words, each one-hot sequence is 50,000 positions long – and almost entirely made up of zeros. This is extremely memory-intensive and computationally inefficient. For this reason, one-hot encoding is often used in natural language processing as a first step, before the long sequences are transformed by a learning process into shorter, denser number sequences – so-called embeddings.

One-hot encoding in products and datasets

In practice, the term is mainly encountered in two contexts. The first is data preparation: anyone preparing their own dataset for a model must convert categorical columns – such as “state” or “payment method” – into numbers. Many software libraries that data analysts use daily have one-hot encoding built in as a function.

The second context is the output of classification models. A model that sorts images into categories – such as a spam filter or image recognition system – often outputs a number sequence that structurally resembles a one-hot sequence. Instead of hard zeros and ones, it contains probabilities: for example [0.02, 0.91, 0.07] for three possible classes. The class with the highest value is the model’s prediction. This comparison with the original one-hot sequence from the training data is also the mechanism by which the model learns during training how far off its prediction was.

Latest News

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.