Encoding high-cardinality string categorical variables

Patricio Cerda, Gaël Varoquaux · DOI

Abstract

Statistical models usually require vector representations of categorical variables, using for instance one-hot encoding. This strategy breaks down when the number of categories grows, as it creates high-dimensional feature vectors. Additionally, for string entries, one-hot encoding does not capture information in their representation. Here, we seek low-dimensional encoding of high-cardinality string categorical variables. Ideally, these should be: scalable to many categories; interpretable to end users; and facilitate statistical analysis. We introduce two encoding approaches for string categories: a Gamma-Poisson matrix factorization on substring counts, and the min-hash encoder, for fast approximation of string similarities. We show that min-hash turns set inclusions into inequality relations that are easier to learn. Both approaches are scalable and streamable. Experiments on real and simulated data show that these methods improve supervised learning with high-cardinality categorical variables. We recommend the following: if scalability is central, the min-hash encoder is the best option as it does not require any data fit; if interpretability is important, the Gamma-Poisson factorization is the best alternative, as it can be interpreted as one-hot encoding on inferred categories with informative feature names. Both models enable autoML on the original string entries as they remove the need for feature engineering or data cleaning.

Introduction: The Challenge of "Dirty" Strings

In real-world tabular data, categorical columns are rarely "clean." A column for "Job Title" might contain "Sr. Software Engineer," "Senior Soft. Eng.," and "Software Engineer IV." Standard one-hot encoding treats these as entirely unrelated categories, leading to two major issues: high dimensionality (creating sparse, computationally expensive vectors) and significant information loss (ignoring morphological similarities between strings).

Examples of high-cardinality, non-curated string categories in drug and employee datasets showing variations and typos.

While data scientists typically solve this with manual cleaning or fuzzy matching, this paper proposes automated alternatives that encode high-cardinality strings into low-dimensional, informative vectors.

Proposed Methods

The authors propose two primary encoding strategies based on character n-grams (subsequences of characters):

  • Min-hash Encoder: Inspired by document indexing, this uses Locality-Sensitive Hashing (LSH). It is stateless, meaning it doesn't need to "fit" on the training data. It maps string inclusions to simple inequalities in a vector space, making it uniquely suited for tree-based models like XGBoost.
  • Gamma-Poisson Factorization: This probabilistic matrix factorization technique treats each string as a "bag-of-n-grams." It decomposes the count matrix into a small number of latent categories. This method is highly interpretable, as each dimension corresponds to a learned "topic" or keyword.

Key Insights: Geometric Inclusion and Interpretability

A critical insight for the Min-hash encoder is its ability to transform logical set inclusions (e.g., "Technician" is part of "Senior Technician") into geometric order relations. This allows machine learning models to easily learn hierarchical relationships within the data.

Visualization of the Min-hash encoder on employee salary data, demonstrating how composite titles like "Senior Supply Technician" relate to their constituent terms.

There is a clear trade-off between the two methods. Min-hash is optimized for speed and scalability, requiring zero training time. Gamma-Poisson is slower but provides human-readable "feature names" (e.g., a dimension might be labeled "Manager, Management, Property"), explaining the model's internal logic.

Experimental Results

The study evaluated these encoders across 17 non-curated real-world datasets and 7 curated benchmarks. Key findings include:

  • Superior Accuracy: N-gram based encoders consistently outperformed one-hot encoding and TF-IDF on "dirty" data.
  • Dimensionality Reduction: Reducing 10,000+ category columns to just 30 or 100 dimensions using these methods improved prediction scores compared to high-dimensional one-hot encoding.
  • Robustness: Character-level n-grams often outperform heavy-duty semantic models like BERT for short categorical strings, as they capture rich morphological cues without requiring extensive context.

Conclusion

These encoding methods remove the need for manual feature engineering or data cleaning on string columns. By integrating these encoders into AutoML pipelines, systems can handle raw tabular data more effectively, providing a "turn-key" solution for high-cardinality categorical variables.

Summary updated

BibTeX
@article{cerda2019encoding,
  author = {Patricio Cerda and Gaël Varoquaux},
  title = {{Encoding high-cardinality string categorical variables}},
  journal = {IEEE Transactions on Knowledge and Data Engineering},
  year = {2019},
  doi = {10.1109/tkde.2020.2992529},
}
Figure