Skip to content

One-Hot Encoding

Easy — everyone uses thisAI & ML

ELI5 - The Vibe Check

Models are bad at math with words, so if a column says red, blue, or green, you can't just feed that in directly. One-hot encoding turns that single column into three new columns, is_red, is_blue, is_green, each a simple 1 or 0, so the model sees clean numbers instead of a category it has no idea how to compare.

Real Talk

One-hot encoding converts a categorical variable with n possible values into n binary columns, where exactly one column is set to 1 for each row and the rest are 0, avoiding the false sense of numeric order that would be implied by simply assigning integers to categories. It's a standard preprocessing step for algorithms that expect numeric input and can't natively interpret categorical features, though it can balloon the number of columns when a category has very high cardinality.

Show Me The Code

import pandas as pd

df = pd.get_dummies(df, columns=['color'], prefix='color')
# color_red, color_blue, color_green columns, each 0 or 1

When You'll Hear This

"Don't just assign 1, 2, 3 to the categories, that implies an order that doesn't exist, one-hot encode it." / "This column has 40,000 unique values, one-hot encoding it would blow up the feature space."

Made with passive-aggressive love by manoga.digital. Powered by Claude.