tezvyn:

Target Encoding: Replacing Categories with Target Stats

AI-drafted, machine-checkedSource: scikit-learn.orgadvanced
Target Encoding: Replacing Categories with Target Stats

Target encoding replaces a category (e.g., "USA") with a statistic from your target variable (e.g., average sales). It's ideal for high-cardinality features where one-hot encoding is impractical. The footgun is data leakage, which causes severe overfitting.

WHY IT EXISTS: Machine learning models need numerical input, but real-world data is full of categorical features like city names or product IDs. One-hot encoding works for a few categories, but for a feature with thousands of unique values (high cardinality), it creates thousands of sparse columns, which is computationally expensive and can hurt model performance. Target encoding was created to solve this.

THE MENTAL MODEL: Think of target encoding as giving your model a "cheat sheet". Instead of just telling the model a data point belongs to category 'A', you replace 'A' with a statistic about that category's relationship to the outcome you're predicting. It's like replacing a student's high school name with the average graduation rate from that school.

HOW IT WORKS: Target encoding replaces each category label with a numerical value derived from the target variable. For a regression task, this is typically the mean of the target for all samples in that category. For a binary classification task, it's the mean of the target (e.g., the frequency of the positive class). The critical footgun is data leakage: if you calculate these means using your entire dataset, the feature will contain information about the very values you're trying to predict, leading to extreme overfitting. To prevent this, you must calculate encodings using only the training data and then apply those values to the validation and test sets. For more robustness, a cross-validation scheme is often used within the training set, and smoothing is applied to prevent rare categories from having unreliable, high-variance encodings.

WHEN TO USE IT: Use target encoding for categorical features with high cardinality, especially with tree-based models like Gradient Boosting or Random Forests. It's effective when you believe there is a direct, strong relationship between the categories and the target variable. It's a staple in competitive machine learning for its ability to extract predictive power from categorical data.

WHEN NOT TO USE IT: Avoid target encoding for low-cardinality features where one-hot encoding is simple, safe, and interpretable. Do not use it if you cannot implement a careful validation strategy to prevent data leakage; the risk of creating a uselessly overfit model is too high. If the relationship between the category and target is noisy or weak, the encoding may just add noise.

ONE CANONICAL EXAMPLE: Predicting ad click-through rates (CTR). A feature might be publisher_id, which has tens of thousands of unique values. Instead of one-hot encoding, you can replace each publisher_id with the average CTR for that specific publisher, calculated carefully on a separate training fold. The model then learns a direct relationship: higher values of this new feature likely mean a higher probability of a click.

Read the original → scikit-learn.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.