Encoding

One-Hot Encoding is the standard technique for converting categorical variables into a binary matrix representation that machine learning models can process — where each unique category becomes its own column with values 0 or 1 (Red → [1,0,0], Blue → [0,1,0], Green → [0,0,1]), avoiding the false ordinal assumption that Label Encoding introduces (Red=0, Blue=1, Green=2 implies Blue is "between" Red and Green), making it the default encoding for linear models and neural networks.

What Is One-Hot Encoding?

  • Definition: A transformation that converts a single categorical column with K unique values into K binary columns — each row has exactly one "1" (hot) and K-1 "0"s (cold), creating a sparse binary representation.
  • Why Not Just Numbers?: If you encode Red=0, Blue=1, Green=2 (Label Encoding), a linear model learns weights where Blue is literally "between" Red and Green mathematically. This is nonsensical for nominal categories. One-hot encoding gives each category its own independent coefficient.

Example

OriginalRedGreenBlue
Red100
Blue001
Green010
Red100

When to Use One-Hot Encoding

Model TypeUse One-Hot?Reason
Linear Regression / LogisticYes (required)Cannot handle nominal categories as integers
Neural NetworksYes (standard)Independent dimensions for each category
SVMYesDistance-based, needs proper encoding
KNNYesDistance calculation needs binary dimensions
Decision Trees / Random ForestOptionalTrees split on individual features, can use label encoding
XGBoost / LightGBMOptionalLightGBM has native categorical support

The High-Cardinality Problem

FeatureUnique ValuesOne-Hot ColumnsProblem
Color33Fine
Country195195Manageable
Zip Code41,000+41,000+Too many columns — model becomes slow, sparse, overfitting
User ID1,000,000+1,000,000+Completely impractical

Solutions for high cardinality:
- Target Encoding: Replace category with mean of target variable.
- Frequency Encoding: Replace category with its count.
- Embeddings: Learn dense vector representations (standard in deep learning).
- Hash Encoding: Map categories to a fixed number of buckets.

The Dummy Variable Trap

  • Problem: With K one-hot columns, the last column is perfectly predictable from the first K-1 (if all are 0, the last must be 1). This creates multicollinearity in linear models.
  • Solution: Drop one column (`drop_first=True` in pandas). Use K-1 columns instead of K.
import pandas as pd
pd.get_dummies(df["color"], drop_first=True)

One-Hot Encoding is the default categorical encoding for most machine learning models — providing each category with an independent dimension that prevents false ordinal assumptions, with the key trade-off being dimensionality explosion for high-cardinality features that requires alternative encoding strategies like target encoding or embeddings.

Take encoding further

Ask the copilot about this term, or have our engineers assess it against your process.