Skip to main content

Categorical feature engineering

Instruction and application
In Progress

Categorical feature engineering

Not all useful data arrives as numbers. Product type, plan name, job title and browser version can all carry important signal, but only if you encode them in a way the model can use.

Pointing illustration

Each encoding method suits a different type of categorical problem.

TechniqueBest used whenKey caution
One-hot encodingLow-cardinality features with no natural orderCan create too many columns for high-cardinality features
Label encodingOrdinal categories or tree-based modelsCan create false order for unordered categories
Target encodingHigh-cardinality features with strong target signalMust guard against leakage with cross-validation
EmbeddingsLarge-scale deep learning tasksLower interpretability
Feature hashingStreaming or open-ended category spacesHash collisions can merge different categories

One-hot encoding

One-hot encoding creates a new binary column for each category. It works well for small, unordered category sets such as subscription plans or device types.

For example, a subscription_type feature with values Basic, Standard and Premium becomes three binary columns.

Label encoding

Label encoding assigns an integer to each category. It is especially useful for ordinal variables like risk level or education level, where the ranking itself carries meaning.

Target encoding

Target encoding replaces a category with a statistic derived from the target, such as mean conversion rate or churn rate. This can be powerful for high-cardinality features like product IDs, but it must be computed carefully to avoid leaking future information.

Embeddings

Embeddings let a deep model learn dense vector representations for large category spaces. They are common in recommendation systems and other settings where similarity between categories matters.

Feature hashing

Feature hashing maps categories into a fixed number of bins with a hash function. This is efficient when you do not know every possible category ahead of time, such as with user-agent strings or campaign names.

Lightbulb icon

Practical rule of thumb

Start with the simplest encoding that fits the data shape. Move to target encoding, hashing or embeddings only when the number of unique categories, the model type or the production setting makes simple methods impractical.

Categorical feature engineering checkpoint
Which categorical features in your work are low-cardinality enough for one-hot encoding, and which ones are not?
Your reflection here...
Where might target encoding be useful, and how would you protect against leakage?
Your reflection here...