liveBook · Manning

Mean target encoding

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A feature encoding technique that replaces each category with a statistic computed from the target values associated with that category.

Mean target encoding converts a high-cardinality categorical feature into numeric values that summarize the target associated with each category. It is useful when IDs, zip codes, product names, or geographical names would create too many one-hot features for practical modeling. section 2.2.5 section 6.1.3

For regression, each category receives its average target value. For classification, it receives a conditional probability or odds ratio. Because rare categories can overfit, smoothing blends the category estimate with the overall training-set target average. section 6.1.3 section 6.1.3

A practical workflow is:

  • Identify categorical features whose cardinality is too large for convenient one-hot encoding; no fixed threshold applies across datasets. section 6.1.3
  • Apply target encoding to the selected columns while retaining other preprocessing choices for lower-cardinality categories. section 6.0
  • Choose a smoothing value above zero to balance category-specific target information against the global target average, then tune it for the dataset. section 6.1.3
  • Prefer target encoding especially with gradient boosting; use embeddings instead when a deep-learning approach is the better fit. section 2.2.5

Pipeline application

The category_encoders TargetEncoder accepts selected high-cardinality columns and a smoothing value, and can be combined with XGBoost and cross-validation in a scikit-learn-compatible pipeline. section 6.1.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines regression and classification statistics, explains smoothing and overfitting risk, and demonstrates pipeline integration without deriving the estimator.
Examples
Uses IDs, zip codes, product names, geographical names, and a binned latitude-longitude coordinates feature.
What it assumes
Assumes categorical features and target values, while distinguishing regression averages from classification probabilities or odds ratios.
What you can do after
Apply it via category_encoders TargetEncoder with selected columns and a tunable smoothing parameter.
How it connects
Connects high-cardinality categorical features with gradient boosting, one-hot encoding, embeddings, and cross-validation.

Also explained in

Compare how 3 books explain mean target encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.