Skip to content

Encoding Categorical Variables

For people

One-hot, ordinal, target and hashing encodings: how to turn categories into numbers without misleading your model.

Editorial team 2 min read

Share X LinkedIn Reddit Bluesky Email

Most models need numbers, but real data is full of categories: country, product type, colour. How you encode them matters.

One-Hot Encoding

Create one binary column per category. colour = red becomes red=1, green=0, blue=0. It makes no assumption about order and is the safe default for categories with a modest number of values. Handle unseen categories at prediction time (handle_unknown="ignore").

Ordinal Encoding

Map categories to integers. Appropriate only when there is a natural order — small < medium < large. For unordered categories it invents a false ranking, though tree models often cope.

Target Encoding

Replace each category with the average target value for that category. Powerful for high-cardinality features such as postcodes, but prone to leakage and overfitting; compute it within cross-validation folds and add smoothing toward the overall mean.

Hashing

Hash categories into a fixed number of columns. Useful for huge or open-ended vocabularies, at the cost of occasional collisions and less interpretability.

Frequency Encoding

Replace each category with how often it occurs. Simple and sometimes surprisingly informative.

Rare Categories

Group very rare categories into an "other" bucket before encoding, so the model isn't fitting to a handful of examples.

Native Support

Some libraries — CatBoost, LightGBM and scikit-learn's histogram gradient boosting — handle categorical features directly and often do it well.

More in Machine learning

All Machine learning guides →
Machine learning Guide · 2 min

Linear Regression Explained

The simplest predictive model: how linear regression fits a line through data, how to read its coefficients, and when it breaks down.

Machine learning 2 min read 25 Sep 2026 For people

Machine learning Guide · 2 min

Logistic Regression for Classification

Despite its name, logistic regression is a classification method. How it produces probabilities and why it remains a strong baseline.

Machine learning 2 min read 24 Sep 2026 For people

Machine learning Guide · 2 min

Decision Trees

How decision trees split data with simple questions, why they are easy to explain, and why single trees overfit.

Machine learning 2 min read 23 Sep 2026 For people

Machine learning Guide · 2 min

Random Forests

Why averaging many randomised decision trees produces a robust, accurate model with little tuning.

Machine learning 2 min read 22 Sep 2026 For people

Discussion 0

Use @handle to bring somebody in, and #tag to file it.

Nothing here yet. Ask a question, or say how you used it.

Sign in to join the discussion. Sign in