Sub topic 10
Redundant columns and the drop option
This chapter assumes you can run OneHotEncoder and get_dummies, and that you know what a linear model's intercept is. It explains why a full set of one-hot columns carries one column too many, and when removing one matters.
Where the redundancy comes from
Each row in a one-hot encoding has exactly one 1 across the columns of a feature. The columns for a feature therefore always add up to 1. Any one column can be computed from the others: it equals 1 minus their sum.
A linear model with an intercept already has a constant term. The intercept is effectively a column of ones, and the sum of the indicator columns equals that column. This is perfect collinearity, sometimes called the dummy variable trap.
When it causes problems
- Unregularized linear or logistic regression: the design matrix is rank deficient. The coefficients are not unique, because you can add a constant to every indicator coefficient and subtract it from the intercept without changing any prediction. Solvers may fail, warn, or return huge unstable values.
- Regularized models (Ridge, Lasso, scikit-learn's default
LogisticRegression): the penalty picks one solution, so nothing breaks. Keeping all columns is common and often preferred, because no category is singled out as the baseline. - Tree-based models: redundancy is harmless. Dropping a column can even make a split on the dropped category harder to express.
Dropping a column with scikit-learn
Set drop="first" to remove the first category of each feature. Categories are sorted alphabetically, so here large is dropped.
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
df = pd.DataFrame({"size": ["small", "medium", "large", "medium"]})
enc = OneHotEncoder(drop="first", sparse_output=False)
print(enc.fit_transform(df))
print(enc.get_feature_names_out())The output is:
[[0. 1.]
[1. 0.]
[0. 0.]
[1. 0.]]
['size_medium' 'size_small']The large row is all zeros. The dropped category becomes the baseline. In a linear model, each remaining coefficient measures the difference from large, and the intercept describes large.
Other drop settings
| Setting | Effect |
|---|---|
| `drop=None` | Keep every column (the default). |
| `drop="first"` | Drop the first category of every feature. |
| `drop="if_binary"` | Drop one column only for features with exactly two categories. Features with more categories keep all columns. |
| `drop=["large"]` | Drop a specific category for each feature. Pass one value per feature, in column order. |
drop="if_binary" is a good middle path. A two-category feature such as yes/no needs only one column, and larger features keep their full set.
In pandas, the equivalent is pd.get_dummies(df, columns=["size"], drop_first=True, dtype=int).
Interaction with unknown categories
Dropping a category makes the all-zeros row mean "the baseline". If you also set handle_unknown="ignore", an unseen category is encoded as all zeros too. The model then treats it exactly like the baseline, and nothing in the output tells the two cases apart.
Choosing a baseline
The model's predictions do not depend on which column you drop. Only the interpretation of the coefficients changes. If you plan to explain coefficients, pass an explicit drop list so the baseline is a meaningful reference, such as the most common category, and not whichever one sorts first.
Exercise
You train an unregularized linear regression on a one-hot encoded city column with three cities and an intercept. Why can the coefficients be unstable, and what is one way to fix it?
Show answer
The three indicator columns sum to 1 in every row, which duplicates the intercept column. The design matrix is rank deficient, so the coefficients are not unique. Fix it by dropping one column (for example drop="first"), or by using a regularized model.
Exercise
After fitting OneHotEncoder(drop="first", handle_unknown="ignore") on training data, a test row has a city that never appeared in training. How is that row encoded, and what is the drawback?
Show answer
It is encoded as all zeros, the same as the dropped baseline city. The model cannot distinguish an unknown city from the baseline, so it predicts as if the city were the baseline.