Home Knowledge Base One-Hot 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?

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:

The Dummy Variable Trap

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.

encodingone hotcategorical

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.