← All questions
Python & DataMedium· Data Preprocessing

How do you decide between one-hot, ordinal, label, and...

The right encoding depends on what the category means and how many distinct values it has. Unordered categories with a manageable number of levels get one-hot encoding — clean for linear models and neural nets, but it explodes in dimensionality once cardinality climbs (10,000 distinct cities becomes 10,000 columns). Genuinely ordered categories (small/medium/large) get ordinal encoding, mapping order to integers directly. Plain label encoding is fine for tree-based models, which never assume the integers imply magnitude or order, but it’s the wrong choice for linear models, which will.

For high-cardinality features where one-hot is impractical, target (mean) encoding replaces each category with the average target value observed for it in training — powerful, but prone to overfitting unless you regularize it with cross-validation (compute the mean using out-of-fold data, not the row’s own target). Across all four methods, one rule never changes: fit the encoder on training data only, and apply that same learned mapping to the test set.

Code playground (Python)