What is the 'Dummy Variable Trap,' and how do you avoid it when encoding categorical features?

Quick Overview

Explain the dummy variable trap in one-hot encoding and how to avoid it in a regression with an intercept. Tests understanding of perfect multicollinearity, reference categories, coefficient interpretation, and which models are affected.

What is the 'Dummy Variable Trap,' and how do you avoid it when encoding categorical features?

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

What is the "dummy variable trap," and how do you avoid it when encoding categorical features? Explain why it breaks a linear regression and how the fix changes the way you read the coefficients. ```hint Add up the columns Write out the one-hot columns for a three-level feature next to the intercept column, then add the dummy columns row by row. ``` ### What a Strong Answer Covers - Why a full set of dummies plus an intercept is perfectly collinear - What that does to $X^TX$ and to the uniqueness of the coefficients - The standard fix, and how the coefficients are interpreted afterwards - The alternative of dropping the intercept, and what happens with several categorical features - Which model families are affected and which tolerate full one-hot encoding ### Follow-up Questions - With two categorical features and no intercept, how many dummy columns can you keep for each? - How does the choice of reference category change the coefficients, the fitted values and the p-values? - Why might you keep all k dummies when fitting ridge regression? - A category shows up in production that the training data never contained. What happens to your encoding?

Overview: Explain the dummy variable trap in one-hot encoding and how to avoid it in a regression with an intercept. Tests understanding of perfect multicollinearity, reference categories, coefficient interpretation, and which models are affected.

Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

What is the "dummy variable trap," and how do you avoid it when encoding categorical features? Explain why it breaks a linear regression and how the fix changes the way you read the coefficients.

What a Strong Answer Covers Guidance

  • Why a full set of dummies plus an intercept is perfectly collinear
  • What that does to XTXX^TX and to the uniqueness of the coefficients
  • The standard fix, and how the coefficients are interpreted afterwards
  • The alternative of dropping the intercept, and what happens with several categorical features
  • Which model families are affected and which tolerate full one-hot encoding

Follow-up Questions Guidance

  • With two categorical features and no intercept, how many dummy columns can you keep for each?
  • How does the choice of reference category change the coefficients, the fitted values and the p-values?
  • Why might you keep all k dummies when fitting ridge regression?
  • A category shows up in production that the training data never contained. What happens to your encoding?