For a categorical variable with K categories, why does keeping all K 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 K one-hot columns always sum to the all-ones intercept column, creating a non-zero null-space vector so X⊤X is singular; adding λI makes X⊤X+λI strictly positive definite, while target encoding replaces categories with out-of-fold smoothed target averages to avoid overfitting.

Steps

  1. Show the dummy-variable trap: let the design matrix X contain an intercept column 1n and K one-hot columns d1,…,dK∈{0,1}n. Because every row belongs to exactly one category, d1+d2+⋯+dK=1n, so Xv=0 for the non-zero vector v=(−1,1,1,…,1)⊤. Thus rank⁡(X) drops by at least 1 and X⊤X is singular.
  2. Explain the OLS fix vs ridge regression: in unregularised linear or logistic regression, dropping one reference category (K−1 dummy columns) restores full rank without changing predictions. However, if you use L2 (ridge) regularisation, X⊤X+λI is strictly positive definite and invertible even with all K columns; moreover, dropping one column in ridge breaks symmetry across categories by shrinking towards the arbitrary reference category rather than the overall average.
  3. Explain target encoding and its precaution: for high-cardinality categories (such as K=10,000 pin codes), one-hot encoding blows up feature dimension. Target encoding replaces category c with an estimate of E[Y∣C=c]. If computed on the entire training fold in-sample, rare categories memorise their own labels yi (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: μ^c=ncyˉc+myˉglobalnc+m, where nc is the category count and m is a smoothing strength.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Last checked on 4 December 2026.

Read this

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 X has mean μ and variance σ2, and each entry is missing independently with probability p (MCAR). If missing entries are replaced by μ, what are the mean and variance of the imputed feature Ximp, how does this affect Corr⁡(Ximp,Y) with a target Y, and why is adding a binary missingness indicator often better?