Frequency and Count Encoding
What you'll learn: How to encode categorical variables by replacing each category with how often it appears in your dataset—a simple, effective technique for high-cardinality features.
The Core Idea
Frequency encoding (also called count encoding) transforms categorical values into numbers based on how frequently each category occurs in your data. Instead of creating dozens of one-hot columns or computing target statistics, you simply count: "How many times does this category appear?"
For example, if you have a "City" column with 50 unique cities, you replace each city name with its occurrence count. If "New York" appears 100 times and "Portland" appears 5 times, every "New York" becomes 100 and every "Portland" becomes 5.
Why It's Useful
When dealing with high-cardinality categorical variables (many unique categories), one-hot encoding creates too many columns, and target encoding risks overfitting. Frequency encoding gives you a single numerical column that captures how common each category is.
The intuition: Frequent categories might behave differently than rare ones. A product bought 10,000 times likely has different characteristics than one bought twice.
Example in Plain English
Imagine encoding "Brand" in a product dataset:
- Brand A appears 500 times → encoded as 500
- Brand B appears 50 times → encoded as 50
- Brand C appears 2 times → encoded as 2
Each brand name is replaced with its count, creating one numerical feature that tells your model about category popularity.
Considerations
Frequency encoding assumes that frequency itself carries predictive information. It works well when popular categories genuinely behave differently from rare ones, but it can't distinguish between categories with identical counts.
Key Takeaway: Frequency encoding replaces categories with their occurrence counts, providing a simple, single-column solution for high-cardinality variables when category frequency matters for prediction.