One-Hot Encoding
noun
say it: “WUN HOT en-KOH-ding” /ˈwʌn ˈhɑt ɛnˈkoʊdɪŋ/
A way of turning a column of categories into numbers by giving each category its own column of 1s and 0s.
What is one-hot encoding?
One-hot encoding turns a category column into one column per category, holding 1 where the row has that category and 0 elsewhere. Models need numbers, and this gives them numbers without inventing an order the categories don't have, as numbering them 1, 2, 3 would.
import pandas as pd
pets = pd.DataFrame({"species": ["dog", "cat", "rabbit"]})
print(pd.get_dummies(pets, dtype=int))
# species_cat species_dog species_rabbit
# 0 0 1 0
# 1 1 0 0
# 2 0 0 1
In a sentence
- “Numbering the species 1, 2 and 3 would invent an order; one-hot encoding doesn't.”
- “One-hot encoding turned the species column into three columns of 1s and 0s.”
Level: Journeyman · More AI Basics terms at this level
Updated