Machine LearningCategorical Data & Feature Engineering

What is target encoding?

Target encoding converts a categorical value into a numerical value calculated from the target variable for that category.

It is often used for categorical features with many unique values, where one-hot encoding would create too many columns.

Simple example

Suppose the feature is:

City = Delhi, Mumbai, Bengaluru

and the target is whether a customer purchased a product.

If the historical purchase rates are:

Delhi     → 0.65
Mumbai    → 0.48
Bengaluru → 0.72

target encoding may replace each city with its corresponding purchase rate.

What statistic is used?

For a regression problem, a common target encoding is the mean target value for each category.

For a binary classification problem, it may be the proportion of positive target values for each category.

The exact statistic depends on the problem.

Why target encoding can be useful

Target encoding can represent a categorical feature using only one numerical column instead of creating one column per category.

This can be useful for high-cardinality features such as:

  • city
  • product ID
  • merchant ID
  • category codes with many unique values

Rare categories need care

A category seen only a few times can produce an unreliable target average.

For this reason, target encoding often uses smoothing, which moves estimates for rare categories closer to the overall target average.

Unseen categories

If a new category appears during validation, testing, or production, a common fallback is the overall target mean or another predefined default.

Target encoding must be computed carefully

Because the encoding uses the target variable, calculating it directly from all available data can leak information into the model.

In practice, training data is often encoded using out-of-fold or similar leakage-safe techniques, while validation and test data are transformed using statistics learned only from training data.

The specific risks of target encoding are covered in the next article.

In short, target encoding replaces a category with a target-based statistic, making it especially useful for high-cardinality categorical features, but it must be calculated carefully to avoid leakage and unreliable estimates.

#what is target encoding#target encoding machine learning#target encoding categorical variables#mean encoding categorical features#target encoding high cardinality#machine-learning#target-encoding#categorical-data#categorical-encoding#feature-engineering

Learning Sequence

Machine Learning Curriculum

30% Completed
Up NextMachine Learning

What risks does target encoding introduce?

Explore Related Questions

Deepen your understanding across related Machine Learning topics.

Machinoai Logo
Machinoai

Your edge in the AI era. Deep dives into machine learning concepts, paper explainers, verified industry jobs, and developer tools.

© 2026 Machinoai. All rights reserved.