Middle — Senior
What is target encoding and what are its problems (overfitting, leakage)?
sobes.tech AI
Answer from AI
Target encoding is a method of encoding categorical features where each category is assigned a numerical value based on the statistics of the target variable (e.g., the mean target value for that category).
Advantages:
- Allows effective use of categorical features in models that work with numerical data.
- Can improve model quality, especially with a large number of categories.
Problems:
- Overfitting: using the mean target over the entire training set can cause the model to memorize specific values, reducing generalization.
- Leakage: calculating target statistics on the entire dataset leaks information from the target into features, distorting model evaluation.
How to avoid problems:
- Use cross-validation or smoothing when calculating target encoding.
- Apply regularization methods, such as adding noise or using global averages.
Example of smoothed target encoding:
# Pseudocode
for each category:
encoded_value = (sum_target_in_category + global_mean * smoothing) / (count_in_category + smoothing)
Thus, target encoding is a powerful tool but requires careful application to avoid overfitting and leakage.