One-Line Summary: One-hot, label, target, and embedding-based encoding -- translating categories into numbers without introducing false relationships.

Prerequisites: Data types and structures, basic linear algebra, exploratory data analysis.

What Is Categorical Encoding?

Imagine teaching a computer to understand the concept of "color." A human intuitively knows that red, blue, and green are distinct categories with no inherent ordering. But machine learning algorithms operate on numbers -- they compute distances, gradients, and dot products. Encoding is the translation layer: converting categorical labels into numerical representations that preserve the true relationships (or lack thereof) among categories while enabling mathematical computation.

Formally, given a categorical feature taking values in a set with cardinality , an encoding is a function that maps each category to a -dimensional numerical vector. The critical design choice is what structure introduces -- or avoids introducing.

How It Works

Label Encoding (Integer Encoding)

Label encoding assigns each category an integer: . If colors are , the encoding might be , , .

When appropriate: Only for genuinely ordinal data where the integer assignment reflects a meaningful order. Examples: education level (high school=1, bachelor's=2, master's=3, PhD=4), satisfaction rating (1-5).

The critical pitfall: For nominal data (no inherent order), label encoding fabricates a metric structure. A linear model receiving , , will "learn" that green is between red and blue, and that the distance from red to blue is twice that from red to green. This is meaningless and harmful. Tree-based models are partially immune because they split on thresholds rather than computing distances, but the arbitrary ordering still constrains the splits the tree can make.

One-Hot Encoding

One-hot encoding maps each category to a binary vector of length with a single 1:

For colors: , , .

This ensures that all categories are equidistant: for . No false ordinal relationships are introduced.

The dummy variable trap: In linear models, if all one-hot columns are included alongside an intercept, the columns are perfectly collinear (they sum to 1 in every row). The solution is dummy encoding: drop one column (the reference category), leaving binary features. The dropped category's effect is absorbed into the intercept.

Cardinality explosion: A feature with categories (e.g., zip codes, product IDs) produces 50,000 new columns. This is computationally prohibitive and creates extremely sparse representations. For high-cardinality features, use alternative encodings below.

Target Encoding (Mean Encoding)

Target encoding replaces each category with the mean of the target variable for that category:

where is the set of observations with category .

This compresses a high-cardinality feature into a single numeric column that directly captures the feature's relationship with the target. However, it introduces a serious risk: target leakage. Each observation's encoding is computed from target values that include its own label.

Regularization strategies to mitigate leakage:

  • Leave-one-out: Exclude the current observation when computing the mean for its category.
  • K-fold target encoding: Split training data into folds; encode each fold using statistics from the other folds.
  • Bayesian smoothing: Blend the category mean with the global mean, weighting by sample size:

where is the category count and is a smoothing parameter. Categories with few observations are pulled toward the global mean, reducing overfitting.

Binary Encoding

Binary encoding converts the integer label to binary representation and uses each bit as a separate feature. For categories, this produces columns instead of .

Example with 8 categories: category 5 integer 5 binary 101 features .

When appropriate: A middle ground between label encoding (too few dimensions, false ordering) and one-hot encoding (too many dimensions). Works well with moderate-to-high cardinality features where the exact category identity matters less than grouping.

Feature Hashing (The Hashing Trick)

Apply a hash function to map categories to a fixed-size feature vector of length , where :

Hash collisions mean multiple categories map to the same bucket, introducing some noise. But for very high cardinality (millions of categories), hashing is the only practical option. It is stateless (no vocabulary to store), handles unseen categories gracefully, and has memory per encoding.

When appropriate: Online learning, extremely high cardinality (user IDs, URL strings), and situations where a small amount of collision noise is acceptable.

Embedding Encoding

Learned dense representations map each category to a low-dimensional continuous vector where . The embedding matrix is trained end-to-end with the model (common in deep learning).

Embeddings capture semantic similarity: categories that behave similarly in predicting the target end up with similar vectors. For example, a product embedding might place "iPhone 15" and "iPhone 14" close together but far from "garden hose."

When appropriate: Deep learning models, high-cardinality features, and situations where you want to capture latent structure among categories. Requires sufficient data to learn meaningful representations.

Choosing the Right Encoding

EncodingCardinalityData typeModel typeProsCons
LabelAnyOrdinal onlyTrees (tolerant)Simple, compactFalse ordering for nominal
One-hotLow (< 15-20)NominalAnyNo false relationshipsDimensionality explosion
DummyLowNominalLinear modelsAvoids collinearitySame explosion risk
TargetHighAnyAnySingle column, captures signalLeakage risk
BinaryMedium-highNominalAnyCompact, no false orderingSome information loss
HashingVery highNominalOnline / large-scaleStateless, fixed memoryHash collisions
EmbeddingHighNominalDeep learningLearns semanticsRequires large data

Why It Matters

Categorical features are everywhere: customer segments, product categories, geographic regions, device types, diagnostic codes. A dataset may have more categorical than numerical features. The encoding choice directly affects model performance, interpretability, and training efficiency. Incorrect encoding (ordinal encoding of nominal data) is one of the most common silent errors in ML pipelines -- the model trains without errors but learns spurious patterns.

Key Technical Details

  • Encode after splitting. Target encoding statistics must be computed on training data only. Even one-hot encoding should learn its category vocabulary from training data; unseen categories in the test set should map to a catch-all "unknown" column (see Data Splitting and Sampling).
  • Handle unseen categories. Production data will contain categories not seen during training. Strategies: map to "unknown," use hashing (which handles novel values by design), or use a fallback value in target encoding (the global mean).
  • Interaction with missing data. A missing category can be treated as its own level ("missing") or imputed before encoding. See Handling Missing Data.
  • Cardinality vs. frequency. A feature with 1,000 categories where 990 appear fewer than 5 times is effectively a 10-category feature with noise. Group rare categories into an "other" bucket before encoding.

Common Misconceptions

  • "One-hot encoding is always the best default." It is safe but not always optimal. For high-cardinality features, it creates sparse, high-dimensional representations that slow training and may degrade performance compared to target or embedding-based encoding.
  • "Label encoding is fine for tree-based models." Trees can work around arbitrary integer assignments, but they are still constrained to threshold splits. A category labeled 5 can only be grouped with categories labeled 1-4 or 6-10 in a single split; arbitrary groupings like {2, 5, 7} require multiple splits, reducing efficiency.
  • "Target encoding always causes leakage." Properly regularized target encoding (K-fold or Bayesian smoothing) controls leakage effectively. The risk comes from naive implementation, not the method itself.
  • "Encoding is a one-time preprocessing step." In production, the encoding scheme must handle evolving category sets, frequency shifts, and new categories gracefully.

Connections to Other Concepts

  • data-types-and-structures.md: Understanding whether a feature is nominal or ordinal is the prerequisite for choosing the correct encoding.
  • exploratory-data-analysis.md: EDA reveals cardinality, frequency distributions, and the relationship between categories and the target -- all of which inform encoding choice.
  • data-cleaning-and-preprocessing.md: Inconsistent string formatting ("New York" vs. "new york") inflates cardinality. Clean before encoding.
  • feature-scaling-and-normalization.md: One-hot encoded features are already on a scale and should not be further standardized. Target-encoded features, however, should be scaled alongside other numerical features.
  • data-splitting-and-sampling.md: Target encoding must be computed per-fold during cross-validation to prevent leakage.

Further Reading

  • Zheng & Casari, Feature Engineering for Machine Learning (2018) -- Practical coverage of encoding strategies with Python examples.
  • Micci-Barreca, "A preprocessing scheme for high-cardinality categorical attributes in classification and prediction problems," SIGKDD Explorations (2001) -- The foundational paper on target encoding with smoothing.
  • Guo & Berkhahn, "Entity Embeddings of Categorical Variables," arXiv (2016) -- Demonstrated that learned embeddings for categorical features outperform one-hot encoding in tabular deep learning.