What is One-Hot Encoding?
One-hot encoding converts categories into binary columns for machine learning. Learn how it works, why it beats label encoding, and how to apply it in Python.
Expected Interview Answer
One-hot encoding converts a categorical variable into a set of binary columns, one per category, where exactly one column is 1 (hot) and the rest are 0 for each row.
It is used because most machine learning algorithms require numeric input and cannot interpret raw category labels. Unlike simple integer (label) encoding, one-hot encoding avoids implying a false ordinal relationship between categories such as red, green, and blue. The trade-off is dimensionality: high-cardinality features create many sparse columns, and dropping one column is common to avoid the dummy-variable trap in linear models.
- Makes categorical data usable by numeric algorithms
- Avoids implying false ordering between categories
- Simple, interpretable, and widely supported
- Works well for low-cardinality nominal features
- Compatible with linear models when one column is dropped
AI Mentor Explanation
Think of a scoreboard light panel with one bulb per dismissal type: bowled, caught, lbw, run out. For any given wicket exactly one bulb lights up and the rest stay dark. One-hot encoding is that panel — each category gets its own on/off slot, and a single row lights precisely one, so the computer reads the dismissal without ranking one type above another.
Step-by-Step Explanation
Step 1
Identify categorical columns
Find the nominal features whose values are labels rather than meaningful numbers.
Step 2
List unique categories
Determine the distinct values in each column to know how many binary columns are needed.
Step 3
Create binary columns
Add one column per category, each holding 0 or 1.
Step 4
Set the hot value
For every row, place a 1 in the column matching its category and 0 elsewhere.
Step 5
Optionally drop one column
Remove one dummy column to avoid the dummy-variable trap in linear models.
What Interviewer Expects
- Clear definition: one binary column per category, exactly one hot
- Why it beats label encoding for nominal data (no false order)
- Awareness of the dimensionality / high-cardinality problem
- Knowledge of the dummy-variable trap and drop_first
- Familiarity with tools like pandas get_dummies or sklearn OneHotEncoder
Common Mistakes
- Using label (integer) encoding for nominal data and implying an order
- One-hot encoding a very high-cardinality feature and exploding dimensions
- Forgetting to drop a column for linear models (dummy-variable trap)
- Fitting the encoder on test data separately, causing train/test column mismatch
- Confusing one-hot encoding with normalisation or scaling
Best Answer (HR Friendly)
“One-hot encoding turns text categories, like colours or cities, into simple yes/no columns so a machine learning model can use them. Each category gets its own column that is 1 when it applies and 0 otherwise, which keeps the model from wrongly assuming the categories have an order.”
Code Example
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
df = pd.DataFrame({'color': ['red', 'green', 'blue', 'green']})
# Quick way with pandas (drop_first avoids the dummy-variable trap)
print(pd.get_dummies(df, columns=['color'], drop_first=True))
# Reusable, leakage-safe way with scikit-learn
enc = OneHotEncoder(sparse_output=False, handle_unknown='ignore')
encoded = enc.fit_transform(df[['color']])
print(enc.get_feature_names_out(['color']))
print(encoded)Follow-up Questions
- How does one-hot encoding differ from label encoding?
- What is the dummy-variable trap and how do you avoid it?
- How do you handle high-cardinality categorical features?
- Why fit the encoder on training data and only transform the test set?
- What alternatives exist, such as target or embedding encoding?
MCQ Practice
1. What is the main reason to prefer one-hot encoding over label encoding for nominal categories?
Label encoding assigns integers that suggest an order; one-hot encoding keeps categories independent and unordered.
2. A drawback of one-hot encoding a high-cardinality feature is:
Each unique category becomes a column, so many categories produce a wide, sparse feature matrix.
3. Dropping one of the one-hot columns primarily helps to:
Keeping all columns makes them perfectly collinear; dropping one removes the redundancy that linear models struggle with.
Flash Cards
What is one-hot encoding? — Turning a category into one binary column per value, with exactly one column set to 1 per row.
One-hot vs label encoding? — Label encoding assigns integers implying order; one-hot keeps nominal categories independent and unordered.
What is the dummy-variable trap? — Perfect collinearity when all one-hot columns are kept; avoided by dropping one column (drop_first).
Main drawback of one-hot encoding? — High-cardinality features produce many sparse columns, inflating dimensionality.