Skip to content

AI Glossary · Concepts

What is One-Hot Encoding?

One-Hot Encoding

One-hot encoding turns a category into numbers a model can use by giving each possible value its own column: the column for the actual value is set to 1 and every other column is 0. A color of red, green or blue becomes [1,0,0], [0,1,0] or [0,0,1].

Also known as: one hot encoding, one-hot vector, dummy variables

· Chain of Thought

Model Training

  • 3 sources

A shipping model has a “destination country” column with values like Denmark, Sweden and Norway. Models do math on numbers, so the text has to be converted. The tempting shortcut is to number them: Denmark 1, Sweden 2, Norway 3. That accidentally tells the model Norway is “three times” Denmark and that Sweden sits between them, which means nothing. One-hot encoding avoids this by creating one column per country, so each country is its own independent switch.

It is a standard step in preparing features for classic models such as linear regression and logistic regression, and it’s part of feature engineering. Two practical issues come up. The encoder needs a rule for categories that appear later but weren’t in the training data. And statistical models with an intercept usually drop one column (“dummy variables”) so the columns aren’t perfectly redundant.

The limit is size. A column with 50,000 product IDs becomes 50,000 mostly-zero columns, and the model can’t tell that two products are similar. Google’s course recommends embeddings in that case: dense vectors that place related items near each other. Language models work this way. Each token ID could in principle be written as a one-hot vector over the whole vocabulary, but the model maps it to a learned embedding instead.

Sources