Quant Memo
Foundational

Frequency and Count Encoding

Frequency (or count) encoding replaces each category with how often it appears in the data, turning a categorical column into a single numeric feature without creating extra columns.

Prerequisites: Ordinal and Label Encoding Pitfalls

Frequency encoding replaces each category in a column with the count (or proportion) of rows in the training data that share that category. If "California" appears in 12,000 out of 100,000 rows, every "California" entry becomes 12,000, or 0.12 if using proportions instead of raw counts. This avoids the dimensionality blow-up of one-hot encoding a high-cardinality column (thousands of cities, product SKUs, or zip codes), since it produces exactly one numeric column no matter how many categories exist.

It works well when a category's frequency is itself informative — rare categories often behave differently from common ones in fraud detection or recommendation systems, for instance. The main pitfall is collision: two entirely different categories that happen to appear the same number of times get mapped to the identical encoded value, and the model has no way to distinguish them anymore.

Frequency encoding compresses any category count into one useful number without exploding column count, but it silently merges any two categories that happen to occur equally often.

Worked example

A dataset has a "merchant_id" column with 50,000 unique merchants. One-hot encoding would create 50,000 new columns. Frequency encoding instead replaces each merchant with its transaction count — a merchant appearing 340 times becomes 340. If a second, completely unrelated merchant also happens to appear exactly 340 times, both now share an identical encoded value, so a model relying solely on this feature can no longer separate the two merchants' distinct behaviors.

Related concepts

Practice in interviews

Further reading

  • Kuhn & Johnson, Feature Engineering and Selection (ch. on categorical predictors)
ShareTwitterLinkedIn