# One-Hot Encoding

> Turns a word into a row of 0s with one 1. Now a model can read it.

**Field:** Artificial Intelligence  
**Category:** Machine Learning  
**Also known as:** One-Hot Vector, Dummy Variables, One-Hot

## Think of it like

It is like a row of light switches. Only one is on at a time.

## Definition

One-hot encoding turns a word category into numbers a model can read. Each possible value gets its own column. The column for the right value holds a 1, and every other column holds a 0. So "red", "green" and "blue" become three columns. Using 1, 2 and 3 instead would wrongly say blue is bigger than red. Many values mean many columns, which is the cost.

## Example

A house table has a city column with London, Paris or Tokyo. After one-hot encoding, a London house reads 1, 0, 0.

## Example code

```python
import pandas as pd
pd.get_dummies(pd.Series(["London", "Tokyo"]), dtype=int)
#    London  Tokyo
# 0       1      0
# 1       0      1
```

Each city gets its own column. A 1 marks the city each row is in.

## Why it matters

Most models only read numbers. This turns words into numbers without making up a false order.

## Common mistake

Using it on a column with thousands of values, like postcodes. The table grows huge and sparse.

## Three principles

- **One column each.** Every value gets its own column.
- **A single 1.** The rest are all 0.
- **No false order.** Blue is not bigger than red.

## Related terms

- [Feature (Machine Learning)](https://learn.benibix.com/term/ai/feature)
- [Feature Engineering](https://learn.benibix.com/term/ai/feature-engineering)
- [Feature Scaling](https://learn.benibix.com/term/ai/feature-scaling)
- [Embedding (AI)](https://learn.benibix.com/term/ai/embedding)
- [Classification (Machine Learning)](https://learn.benibix.com/term/ai/classification)

## Taught in

- [Feature Engineering](https://learn.benibix.com/learn/ai-machine-learning/feature-engineering): Machine Learning, 4/18

![A raw row with city Paris, size 50 square metres and price 600 becomes a ready row. City turns into three columns, London 0, Paris 1, Tokyo 0, marked one-hot. A new column, price per square metre, reads 12. Price is scaled to 0.67. A label column reading sold fast is added by a person.](https://learn.benibix.com/diagrams/ai3-data-prep.svg)

---

Source: https://learn.benibix.com/term/ai/one-hot-encoding
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
