A "city" column has 4,000 distinct values. Which encoding usually works better than one-hot for a tree model?
- Exam domain
- Data Manipulation and Preparation23% of the exam
- Difficulty
- Medium
- In this mock
- 11 questions on this domainPractise this domain
Why the exam asks this
Data Manipulation and Preparation (23% of the exam)
Objective in the exam guide: Feature engineering for numerical and categorical variables
Feature engineering for categorical variables is named.
Why C is the answer
Target or count encoding computed on training folds only
High-cardinality categories make one-hot sparse and wide. Target or count encoding gives one compact column; computing it on training folds avoids leakage.
Remember: High-cardinality categories: target or count encoding.
How it works
Numeric features may be scaled or transformed; categorical features are encoded by one-hot, ordinal, target or count methods.
In detail
- High cardinality suits target or count encoding.
- Compute on training data only.
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. Converting city names to random numbers each runWrong
Random numbers change each run and add noise.
B. One-hot encoding into 4,000 new columns, one for each city in the datasetWrongMost tempting
4,000 columns are sparse and slow.
Why it tempts: One-hot is the textbook default.
C. Target or count encoding computed on training folds onlyRight
High-cardinality categories: target or count encoding.
D. Dropping the columnWrong
Dropping loses signal.
Use case
An illustrative scenario, not a real incident.
Illustrative: target-encoding city improves a fraud model's AUC by 0.02 over one-hot.
See it in different ways
flowchart TD
Q{"How many categories?"}
Q -->|"Few"| A0["One-hot"]
Q -->|"Many"| A1["Target or count encoding"]
Q -->|"Ordered"| A2["Ordinal encoding"]How to read this chart
- At "How many categories?": if "Few", go to "One-hot"; if "Many", go to "Target or count encoding"; if "Ordered", go to "Ordinal encoding".
mindmap
root(("Features"))
n1["Numeric"]
n2["Scaling"]
n3["Binning"]
n4["Log transform"]
n5["Categorical"]
n6["One-hot"]
n7["Target encoding"]
n8["Count encoding"]
n9["Time"]
n10["Day of week"]
n11["Lag features"]How to read this chart
- The centre is "Features".
- "Numeric" covers "Scaling", "Binning", "Log transform".
- "Categorical" covers "One-hot", "Target encoding", "Count encoding".
- "Time" covers "Day of week", "Lag features".