AI

One-hot encoding explained

One-hot encoding explained: why numbering categories misleads a model, how each category gets its own 0 or 1 column, and what to use when there are too many.

  • 7 min read
  • 5 questions
  • +35 XP
Transcript

One-hot encoding, explained by cats. Kitty has a column called favorite fish. Tuna. Salmon. Sardine. But the machine doesn’t understand fish words. It understands numbers. Kitty could say Tuna equals 1. Salmon equals 2. Sardine equals 3. But that causes trouble.

The machine may think 3 is bigger than 1. Or Sardine is somehow more important than Tuna. That’s not true. These are just labels. Not rankings. So Kitty uses one-hot encoding. She makes a new box for each fish. Tuna.

Salmon. Sardine. If the row says Tuna, then Tuna gets 1. Salmon gets 0. Sardine gets 0. Only one box is hot. The rest stay cold. Tuna becomes: 1, 0, 0 Salmon becomes: 0, 1, 0 Sardine becomes: 0, 0, 1 Now the machine doesn’t invent order where none exists.

Each category gets its own little flag. Very clear. Very safe. But if Kitty has a thousand fish types, she gets a thousand boxes. So one-hot encoding is great for small categorical data. Simple and clean.