2.7 One-Hot Encoding and When It Is or Is Not Appropriate
Key Takeaways
- One-hot encoding represents a nominal feature as binary indicator columns, imposing no artificial ordering between levels.
- It is appropriate for low-to-moderate cardinality nominal features feeding linear, logistic, distance-based, or neural models.
- It is a poor fit for high-cardinality features, where it explodes dimensionality, and for tree ensembles, where sparse binary splits weaken each tree.
- Ordinal features should keep their order via integer encoding; one-hot encoding discards the ranking information the model could use.
- In Spark ML the sequence is `StringIndexer` → `OneHotEncoder` → `VectorAssembler`, with `handleInvalid="keep"` protecting inference against unseen categories.
2.7 One-Hot Encoding and When It Is or Is Not Appropriate
Raw enterprise data in the Lakehouse is rarely formatted as pure numerical vectors ready for estimator ingestion. Categorical variables (such as zip codes, device types, education levels, and transaction statuses) must be mathematically transformed into numeric representations, and continuous numeric features of varying scales must be standardized or normalized.
Mastering feature transformation pipelines—and understanding how they behave in distributed Spark ML vs. single-node scikit-learn environments—is a critical domain on the Databricks Machine Learning Associate exam.
Categorical Data Taxonomy: Nominal vs. Ordinal
Before selecting an encoding strategy, the data scientist must identify whether categorical features possess an inherent mathematical ordering:
CATEGORICAL DATA TYPES
│
┌────────────────────────────┴────────────────────────────┐
▼ ▼
NOMINAL ORDINAL
(No Natural Order) (Explicit Hierarchy)
• State: [CA, NY, TX] • Education: [High School < BS < MS < PhD]
• Device: [iOS, Android, Web] • Customer Tier: [Bronze < Silver < Gold]
• Encoding: One-Hot Encoding, • Encoding: Ordinal Integer Mapping
Target Encoding, Frequency Encoding (e.g., Bronze=1, Silver=2, Gold=3)
Exam Trap: Applying simple integer/label encoding (e.g.,
Red=1, Green=2, Blue=3) to nominal variables imposes an artificial linear hierarchy. A linear regression model would incorrectly assume thatBlueis three times the value ofRedand thatGreenis the exact mathematical midpoint. Use One-Hot Encoding for nominal data.
Categorical Encoding Techniques & Tradeoffs
| Encoding Technique | Best Suited For | Spark ML Transformer | Advantages | Drawbacks / Risks |
|---|---|---|---|---|
| One-Hot Encoding (OHE) | Low-to-moderate cardinality nominal variables ($< 50$ distinct categories) | OneHotEncoder (after StringIndexer) | No artificial ordering imposed; preserves distinct category identities. | High cardinality causes dimensionality explosion and extreme memory sparsity. |
| Ordinal / Integer Encoding | Explicitly ordered ordinal variables | Custom mapping or StringIndexer | Preserves ordering; compact single-column representation without expanding dimensionality. | Imposes linear spacing between ordinal ranks that may not reflect true intervals. |
| Target / Impact Encoding | High-cardinality nominal variables (e.g., zip codes, cities) | Custom PySpark aggregation or category_encoders | Replaces category with mean target value; maintains single-column dimensionality. | Severe risk of target leakage and overfitting without k-fold out-of-fold smoothing. |
| Frequency / Count Encoding | High-cardinality features | Custom PySpark groupBy().count() | Replaces category with its occurrence count/frequency; captures prevalence. | Different categories with identical frequencies receive identical encoded values. |
| Top-K + Other Grouping | Long-tailed nominal categories | Custom PySpark when().otherwise() | Retains top $K$ most frequent categories and consolidates the long tail into "Other". | Discards granular category distinctions in the tail. |
When One-Hot Encoding Is the Wrong Tool
The exam asks explicitly about the model types and datasets where one-hot encoding is inappropriate. There are four recognised cases.
High-cardinality features
A zip_code column with 40,000 levels becomes 40,000 sparse columns. Memory and
training time grow with the level count, most columns are almost always zero, and each
individual indicator carries too little signal to be selected by a model. Prefer target
encoding with out-of-fold smoothing, frequency encoding, hashing, or a top-K plus
"Other" grouping.
Ordinal features
Bronze < Silver < Gold carries an ordering that a model can exploit with a single
integer column. One-hot encoding erases that ordering and spends three columns to
convey less information.
Tree-based models
A decision tree splits one column at a time. With one-hot encoding, a single
informative categorical feature becomes many binary columns, and each split can only
separate one level from the rest — so the tree needs greater depth to express what a
single multi-way categorical split would capture, and under column subsampling
(colsample_bytree, max_features) the individual indicators are often not even
sampled. LightGBM and CatBoost handle categorical columns natively and generally do
better with the raw or index-encoded column.
Features that are really identifiers
A near-unique column such as order_id produces one indicator per row. It cannot
generalise and should be dropped, not encoded.
| Model family | One-hot encoding? |
|---|---|
| Linear / logistic regression, Ridge, Lasso | Yes — required; integer codes would imply a false ordering |
| k-NN, K-Means, SVM | Yes — distances need each level to be equidistant |
| Neural networks | Yes for low cardinality; embeddings for high cardinality |
| Random forest, Spark GBT | Workable, but index encoding is usually preferable |
| LightGBM, CatBoost | No — use native categorical support |
Distributed Categorical Pipelines in PySpark ML
In Apache Spark ML, categorical transformation is typically executed in three sequential stages:
StringIndexer: Maps string categorical values to numerical index columns ordered by category frequency (the most frequent category receives index0.0).OneHotEncoder: Converts category indices into binary vector representations (SparseVector).VectorAssembler: Combines multiple numeric features and one-hot encoded vectors into a single unifiedfeaturesvector column required by Spark ML estimators.
[Raw Strings] ──> StringIndexer ──> [Category Indices] ──> OneHotEncoder ──> [Sparse Vectors] ──> VectorAssembler ──> [features Vector]
Handling Unseen Categories at Inference Time (handleInvalid)
When a model deployed in production encounters a category string that was never observed during training, StringIndexer must know how to respond. The handleInvalid parameter controls this behavior:
handleInvalid="error"(Default in some versions): Throws a runtime JVM exception and halts execution. Unsafe for real-time production pipelines.handleInvalid="skip": Filters out and discards the entire row containing the invalid/unseen category. Useful for batch training, but dangerous in real-time inference endpoints.handleInvalid="keep"(Recommended for Production): Maps all unseen/invalid categories into a dedicated extra bucket (index equal to the number of distinct training categories). This allows downstream one-hot encoders and models to score new categories gracefully.
Complete End-to-End PySpark ML Pipeline
from pyspark.ml import Pipeline
from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler
categorical_cols = ["contract_type", "payment_method", "internet_service"]
numeric_cols = ["age", "tenure_months", "monthly_charges"]
# Step 1: Index categorical strings to frequency-ranked numbers
indexers = [
StringIndexer(
inputCol=c,
outputCol=f"{c}_idx",
handleInvalid="keep", # Keep unseen categories in separate bucket
stringOrderType="frequencyDesc" # Most frequent category = index 0.0
)
for c in categorical_cols
]
# Step 2: One-Hot Encode indexed columns into SparseVectors
encoder = OneHotEncoder(
inputCols=[f"{c}_idx" for c in categorical_cols],
outputCols=[f"{c}_vec" for c in categorical_cols],
dropLast=True, # Drop last category to avoid dummy variable trap
handleInvalid="keep"
)
# Step 3: Assemble all continuous and encoded vector columns into single features column
assembler = VectorAssembler(
inputCols=numeric_cols + [f"{c}_vec" for c in categorical_cols],
outputCol="features",
handleInvalid="keep"
)
# Step 4: Construct and fit the unified Pipeline
pipeline = Pipeline(stages=indexers + [encoder, assembler])
pipeline_model = pipeline.fit(train_df)
# Transform train and test splits
train_prepared = pipeline_model.transform(train_df)
test_prepared = pipeline_model.transform(test_df)
The
dropLastParameter: In PySpark'sOneHotEncoder,dropLast=True(default) drops the last category entry, producing a vector of length $k-1$ for $k$ categories. This prevents the dummy variable trap (exact multi-collinearity) in unregularized linear models. For tree-based models or regularized linear models,dropLast=Falsecan be used.
Feature Scaling Techniques & Formulations
Continuous features often exhibit drastically different numerical ranges (e.g., age between 18 and 90 vs. annual_income between $20,000 and $500,000). Features with large magnitudes can dominate distance metrics and gradient steps.
Raw Features: Age: [18 ── 90] Income: [$20,000 ──────────── $500,000]
StandardScaler: Age: [-1.8 ── +2.1] (mean=0, std=1) Income: [-1.5 ── +3.2] (mean=0, std=1)
MinMaxScaler: Age: [0.0 ── 1.0] Income: [0.0 ──────────────── 1.0]
RobustScaler: Age: [-1.2 ── +1.5] (median/IQR) Income: [-0.9 ── +2.4] (median/IQR)
Mathematical Formulations
- Standardization (
StandardScaler): Centers features at $\mu = 0$ with unit variance $\sigma = 1$. Does not bound values into a fixed range. Outliers are preserved. PySpark Note:StandardScaler(withMean=True, withStd=True). SettingwithMean=Trueon sparse vector inputs converts them to dense vectors, which can consume massive memory. - Min-Max Normalization (
MinMaxScaler): Compresses all feature values strictly into $[0.0, 1.0]$ (or custom bounds). Highly sensitive to extreme outliers, which compress standard observations into a narrow band. - Robust Scaling (
RobustScaler): Uses median and Interquartile Range ($Q3 - Q1$). Resilient to outliers because centering and scaling metrics are not influenced by extreme tails.
Algorithm Scaling Sensitivity Comparison
| Algorithm Family | Requires Feature Scaling? | Operational Reason |
|---|---|---|
| k-Nearest Neighbors (k-NN) | Mandatory | Computes Euclidean distance $\sqrt{\sum (x_a - x_b)^2}$. Unscaled features with large ranges dominate neighbor selection entirely. |
| K-Means Clustering | Mandatory | Cluster centroids are calculated using Euclidean distance. Unscaled features warp spherical cluster geometries into elongated ellipsoids. |
| Support Vector Machines (SVM) | Mandatory | Maximizes the margin separating support vectors. Distance to the separating hyperplane is distorted by unscaled features. |
| Regularized Linear Models (Lasso $L_1$, Ridge $L_2$) | Mandatory | Penalty terms $\lambda \sum \lvert w_j \rvert$ or $\lambda \sum w_j^2$ penalize all weights equally. Unscaled features receive artificially small weights and escape penalization. |
| Neural Networks (MLP / Deep Learning) | Mandatory | Accelerates gradient descent convergence; prevents vanishing/exploding gradients in activation functions. |
| Unregularized Linear / Logistic Regression (OLS) | Recommended (Not strictly mandatory) | Scale does not change the optimal analytical hyperplane, but scaling dramatically improves gradient descent optimization speed. |
| Decision Trees, Random Forests, XGBoost, LightGBM | Scale-Invariant (Not Required) | Splits evaluate only the relative ordering of values along one feature at a time. Multiplying or shifting feature values does not alter optimal split points. |
A dataset contains an unordered categorical feature 'payment_method' with 4 distinct levels: 'Credit Card', 'Bank Transfer', 'Electronic Check', and 'Mailed Check'. What is the most statistically sound method to encode this feature for an unregularized Logistic Regression model?
A production PySpark streaming pipeline uses a trained StringIndexer model to preprocess incoming web logs. If a user with a previously unseen browser type is encountered in the stream, which StringIndexer configuration ensures the pipeline processes the event without throwing an exception or dropping the row?
A data science team is preparing features for two distinct machine learning models: a Ridge Regression regressor (L2 regularization) and an XGBoost Classifier. Which statement correctly describes the feature scaling requirement for these models?
A dataset contains a merchant_id column with roughly 85,000 distinct values. A data scientist plans to one-hot encode it for a gradient boosted tree model. What is the strongest objection?