What's the difference between dummy variable and one-hot encoding?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Dummy variable encoding and one-hot encoding both convert categorical variables into numeric format for machine learning models, but they differ in the number of columns created. One-hot encoding creates k binary columns for k categories. Dummy variable encoding creates k-1 columns, dropping one category as a reference (the "dummy variable trap" avoidance). Use dummy variables for linear regression to avoid multicollinearity. Use one-hot encoding for tree-based models and neural networks where multicollinearity is not an issue.
One-Hot Encoding
Creates one binary column per category:
For 3 categories (Red, Blue, Green), one-hot encoding creates 3 columns. Each row has exactly one 1 and the rest 0.
Dummy Variable Encoding
Creates k-1 columns, dropping one reference category:
The dropped category (Blue) is implicitly represented when all other columns are 0. This prevents the "dummy variable trap."
The Dummy Variable Trap
Multicollinearity inflates coefficient standard errors, making them unreliable. Dropping one category breaks the linear dependency.
Side-by-Side Comparison
When to Use Each
Use Dummy Variables (k-1)
Use dummy variables for:
- Linear regression
- Logistic regression
- Any model that assumes feature independence
- When interpretability of coefficients matters
Use One-Hot Encoding (k)
Use one-hot encoding for:
- Decision trees and random forests
- Neural networks
- K-nearest neighbors
- Any model that does not assume feature independence
Scikit-Learn Implementations
Multiple Categorical Columns
Common Pitfalls
- Using one-hot encoding with linear regression: Creates multicollinearity (the dummy variable trap). The model either fails to converge or produces unstable coefficients. Always use
drop_first=Truefor linear models. - Dropping the wrong reference category: The dropped category becomes the baseline for interpreting coefficients. Choose a meaningful reference (e.g., "control group" or most common category) for better interpretability.
- High cardinality: A column with 1,000 unique values creates 1,000 (or 999) new columns. This causes memory issues and overfitting. Use target encoding, frequency encoding, or embeddings for high-cardinality features.
- New categories at prediction time: If the test set has a category not seen during training (e.g., "Purple" when training only had Red/Blue/Green), the encoder fails. Set
handle_unknown="ignore"inOneHotEncoderto output all zeros for unknown categories. - Ordinal variables: Encoding ordered categories (Small < Medium < Large) as one-hot loses the ordering. Use ordinal encoding (
0, 1, 2) instead to preserve the rank relationship.
Summary
- One-hot encoding creates
kbinary columns forkcategories — one column per category - Dummy variable encoding creates
k-1columns, dropping one reference category - Use dummy variables (
drop_first=True) for linear/logistic regression to avoid multicollinearity - Use one-hot encoding for tree-based models and neural networks
- In pandas, use
pd.get_dummies(drop_first=True)for dummy encoding - In scikit-learn, use
OneHotEncoder(drop="first")for dummy encoding
Related reading
- What's the difference between 'feed forward network' and 'fully-connected network'?
- What''s the difference between GradientTape, implicit_gradients, gradients_function and implicit_value_and_gradients?
- What's the difference between input_shape and batch_input_shape in LSTM
- What's the difference between LibSVM and LibLinear
- What's the difference between scikit-learn and tensorflow? Is it possible to use them together?
- What's the meaning of logistic regression dataset labels?
- What's the difference between LibSVM and LibLinear
- What's the difference between LSTM and LSTMCell?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.