liveBook · Manning

High-cardinality categorical feature

As explained in Ensemble Methods for Machine Learning

A high-cardinality categorical feature is a categorical input variable with a large number of distinct categories.

A high-cardinality categorical feature has many distinct, nonnumeric categories. ZIP code illustrates the challenge: the United States contains 41,692 unique ZIP codes, far more than its 50 states. section 8.0

High-cardinality string features often contain duplicate meanings, alternate spellings, and typos. Exact matching creates still more categories, while one-hot encoding expands the feature space and ordinal encoding may invent an unjustified order. section 8.4

A practical workflow combines diagnosis, encoding choice, and held-out evaluation:

  • Count unique values, then separate low-cardinality categorical columns, high-cardinality columns, and continuous features. section 8.4
  • Prefer compact encodings when one-hot columns would become numerous and sparse; use similarity rather than exact matching for related strings. section 8.4 section 8.5
  • Compare SimilarityEncoder, MinHashEncoder, and GapEncoder inside a modeling pipeline rather than assuming one method will perform best. section 8.4
  • Use a train/test split and compare predictive scores to assess whether the representation preserves useful information. section 8.4

Employee salary data

An employee-salary data set contains 9,211 rows. The employee_position_title feature has 385 categories, compared with 2 for gender and 37 for department_name. Treat the title as high-cardinality, encode it with SimilarityEncoder, MinHashEncoder, or GapEncoder, and compare the resulting XGBoost regressors. Reported R² scores are approximately 0.900 for SimilarityEncoder and MinHashEncoder, and 0.890 for GapEncoder. section 8.4

import pandas as pd
df = pd.read_csv('./data/ch08/employee_salaries.csv')
df.head()

Inspect the employee-salary data directly before counting categories and assigning high-cardinality encoders. section 8.4

How Ensemble Methods for Machine Learning covers it

How deep it goes
Defines cardinality, explains sparsity and dirty strings, and demonstrates three similarity or hashing encoders in a predictive pipeline.
Examples
Uses ZIP codes, employee job titles, and the Adult data set’s 30-category native-country feature as examples.
What it assumes
Assumes basic supervised-learning features and labels while reinforcing categorical-versus-continuous inputs and numeric encoding requirements.
What you can do after
Apply dirty_cat encoders in a scikit-learn pipeline and compare held-out regression scores.
How it connects
Connects categorical features with one-hot encoding, ordinal encoding, string similarity, hashing, and ensemble learning.

Also explained in

Compare how 2 books explain high-cardinality categorical feature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Ensemble Methods for Machine Learning. The section links point to the parts of the book it draws on.