tezvyn:

One-Hot Encoding: Turning Categories into Numbers

AI-drafted, machine-checkedSource: Wikipedia: One-hotbeginner

One-hot encoding turns categories into on/off switches for algorithms. Instead of one column with "red" or "green", you get separate "is_red" and "is_green" columns. It's essential for machine learning, but avoid it for features with too many unique values.

WHY IT EXISTS: Machine learning models are fundamentally mathematical; they operate on numbers, not text labels like "cat", "dog", or "fish". We need a way to represent these categorical choices numerically without accidentally implying a false relationship, like that 2 ("dog") is greater than 1 ("cat"). One-hot encoding solves this by creating independent, non-ordered numerical features.

THE MENTAL MODEL: Think of it like a survey with mutually exclusive options. Instead of writing down "Option C", you have a sheet with boxes for A, B, and C, and you just put a checkmark in the 'C' box. Each option gets its own binary representation (checked or unchecked), and only one can be "on" at a time for a given data point. The original category is represented by a vector of bits with only a single '1' (the "one-hot").

HOW IT WORKS: Take a column of data with 'N' unique categories, like a 'Color' column with values 'Red', 'Green', and 'Blue'. One-hot encoding replaces this single column with 'N' new columns: 'Color_Red', 'Color_Green', and 'Color_Blue'. For a row that was originally 'Red', the new values will be [1, 0, 0]. For 'Green', it becomes [0, 1, 0]. Each original value is converted into a vector of zeros with a single '1' at the index corresponding to that value.

WHEN TO USE IT: Use one-hot encoding for nominal categorical features, where there is no intrinsic order between the categories ('Red' is not greater than 'Blue'). It is a standard, safe choice for features with a low number of unique values (low cardinality) and works well with most machine learning models, especially linear models and neural networks.

WHEN NOT TO USE IT: Avoid using it on features with a high number of unique values (high cardinality), like 'user_id' or 'zip_code'. A feature with 10,000 unique values would create 10,000 new columns, leading to a massive, sparse dataset that consumes memory and can degrade model performance. Also, for ordinal data where order matters ('small', 'medium', 'large'), a simple integer mapping might be more efficient, though it can also mislead some models.

ONE CANONICAL EXAMPLE: A dataset for predicting house prices has a 'Neighborhood' column with three options: 'Downtown', 'Suburbs', 'Uptown'. To prepare this for a regression model, you one-hot encode it. The 'Neighborhood' column is dropped and replaced by three new columns: 'is_Downtown', 'is_Suburbs', and 'is_Uptown'. A house in the 'Suburbs' would have the values [0, 1, 0] across these three new features, clearly indicating its category without implying any numerical ranking.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.