For a categorical variable with categories, why does keeping all one-hot indicator columns together with an intercept make unregularised linear regression singular, why does ridge regression remove this singularity, and what precaution is required when using target encoding instead?
Foundations, Feature engineering
Level L3 Intermediate. Length Medium. Nature Th+Mat Both.
What's being asked
Whether you understand the dummy-variable trap algebraically, how L2 regularisation changes it, and how target encoding causes label leakage if computed naively.
Key idea
The one-hot columns always sum to the all-ones intercept column, creating a non-zero null-space vector so is singular; adding makes strictly positive definite, while target encoding replaces categories with out-of-fold smoothed target averages to avoid overfitting.
Steps
- Show the dummy-variable trap: let the design matrix contain an intercept column and one-hot columns . Because every row belongs to exactly one category, , so for the non-zero vector . Thus drops by at least and is singular.
- Explain the OLS fix vs ridge regression: in unregularised linear or logistic regression, dropping one reference category ( dummy columns) restores full rank without changing predictions. However, if you use L2 (ridge) regularisation, is strictly positive definite and invertible even with all columns; moreover, dropping one column in ridge breaks symmetry across categories by shrinking towards the arbitrary reference category rather than the overall average.
- Explain target encoding and its precaution: for high-cardinality categories (such as pin codes), one-hot encoding blows up feature dimension. Target encoding replaces category with an estimate of . If computed on the entire training fold in-sample, rare categories memorise their own labels (severe target leakage). You must compute target means out-of-fold (using only the other folds' labels) and smooth rare categories towards the global target mean: , where is the category count and is a smoothing strength.
Common mistakes
- Dropping a dummy column out of habit even when training a decision tree or a strongly L2-regularised model, where keeping all columns is symmetric and preferable.
- Computing target-encoded values before splitting into cross-validation folds or without out-of-fold computation, which leaks training labels into features.
How long your answer should be
Half a page; 4–5 minutes.
Last checked on 4 December 2026.
Read this
- scikit-learn: OneHotEncoder: The drop parameter documentation explains how dropping one category avoids perfectly collinear indicator columns in unregularised models.
Previous in Foundations: How does the minimum description length (MDL) principle turn model selection into a data-compression problem, and how does a two-part MDL code connect mathematically to MAP estimation and BIC?
Next in Foundations: Suppose a feature has mean and variance , and each entry is missing independently with probability (MCAR). If missing entries are replaced by , what are the mean and variance of the imputed feature , how does this affect with a target , and why is adding a binary missingness indicator often better?