Regularized target encoding outperforms traditional methods in supervised machine learning with high cardinality features

Florian Pargent, Florian Pfisterer, Janek Thomas, Bernd Bischl

Introduction

In the landscape of supervised machine learning, categorical variables present a significant challenge, particularly when they exhibit high cardinality—meaning they contain hundreds or thousands of unique levels. Common examples include zip codes, city names, or unique product identifiers. Most machine learning algorithms are designed to process numerical inputs, necessitating a transformation step known as encoding.

Traditional approaches like one-hot encoding, which creates a binary column for every unique level, often succumb to the "curse of dimensionality." In high-cardinality scenarios, this leads to massive, sparse datasets that increase computational costs and heighten the risk of overfitting. This paper explores an alternative: regularized target encoding, demonstrating its superiority across diverse datasets and algorithms.

Categorizing Encoding Techniques

The researchers categorize encoding methods into two primary groups: target-agnostic and target-based. Target-agnostic methods, such as one-hot, frequency, or hash encoding, do not consider the relationship between the feature and the target variable. In contrast, target-based methods leverage this relationship to create more informative numerical representations.

Taxonomy of common categorical variable encoding techniques

Among target-based methods, Generalized Linear Mixed Model (GLMM) encoding emerges as a powerful tool. By treating categorical levels as "random effects," GLMMs naturally shrink estimates for rare levels toward the global mean. This built-in regularization is crucial for handling categories with few observations, preventing the model from making overconfident predictions based on insufficient data.

Experimental Setup

To validate these methods, the authors conducted a comprehensive benchmark using 24 diverse datasets from the OpenML platform. These included:

  • 8 Regression problems (e.g., housing prices, sales forecasting)
  • 10 Binary classification problems (e.g., employee access, credit risk)
  • 6 Multiclass classification problems (e.g., species classification, product categorization)

The datasets were specifically selected for their high-cardinality features, some of which contained over 30,000 unique levels. The study compared various encoding strategies across several popular machine learning algorithms, including Lasso, Random Forest, Gradient Boosting, and K-Nearest Neighbors (KNN).

Key Findings

The benchmark results highlight several critical insights for practitioners:

  1. The Superiority of GLMM with Cross-Validation: The combination of GLMM encoding and 5-fold cross-validation (CV) consistently outperformed other methods. The use of CV is vital; it ensures that the encoding for a specific data point is calculated using only other parts of the training set, effectively insulating the model from "data leakage."
  2. The Danger of Simple Target Encoding: Target encoding without proper regularization or cross-validation (often called "impact encoding") frequently performed worse than simply removing the high-cardinality features. This underscores the lethal nature of data leakage in predictive modeling.
  3. Efficiency and Performance: While calculating GLMMs adds a step to the preprocessing pipeline, the resulting reduction in dimensionality often leads to faster overall training times compared to one-hot encoding. Furthermore, even models with native categorical support, like Random Forests, benefited from pre-encoding with target-based methods.
  4. One-Hot Encoding's Niche: One-hot encoding remained competitive only in scenarios with relatively low cardinality or when paired with models like Lasso that perform internal feature selection.

Conclusion and Future Directions

This research establishes regularized target encoding, particularly GLMM-based approaches, as a robust standard for handling high-cardinality data. By balancing information density with rigorous regularization, these methods provide a scalable solution to the dimensionality challenges inherent in modern datasets.

While the study highlights the efficiency gains of these methods, it also notes several areas for future exploration. The benchmark focused on classical machine learning and did not include deep learning techniques like entity embeddings. Additionally, the focus was on univariate encoding; investigating "interaction encoding"—where combinations of categories are encoded together—could yield further performance gains. Finally, the study treated all categories as unordered, leaving the specific challenges of high-cardinality ordinal data for future research.

Summary updated

BibTeX
@article{pargent2022regularized,
  author = {Florian Pargent and Florian Pfisterer and Janek Thomas and Bernd Bischl},
  title = {{Regularized target encoding outperforms traditional methods in supervised machine learning with high cardinality features}},
  journal = {Comput. Stat.},
  year = {2022},
  doi = {10.1007/s00180-022-01207-6},
}
Figure