Back to Blog
Lesson 6 of the Intermediate Machine Learning: Real-World Pipelines course
AI/MLJune 25, 20263 min read

Encoding Categorical Variables: Production Pipelines

Master categorical encoding in your ML pipelines. Learn when to use OneHot vs. Ordinal encoding and how to implement target encoding without data leakage.

categorical encodingmachine learningscikit-learnfeature engineeringdata scienceaimachine-learningpython

Previously in this course, we discussed ColumnTransformer for Heterogeneous Data to isolate feature processing. In this lesson, we move from structural organization to the numerical transformation of categorical data.

Most machine learning algorithms are essentially massive mathematical functions; they cannot ingest strings or categories directly. Converting these into numbers—categorical encoding—is a foundational step in your feature engineering workflow.

OneHotEncoder vs. OrdinalEncoder: The Core Trade-off

The two most common strategies in Scikit-Learn are OneHotEncoder and OrdinalEncoder. Choosing the wrong one can lead to poor model convergence or spurious relationships.

OneHotEncoder creates a binary column for every unique category. It assumes no inherent order.

  • Best for: Nominal data (e.g., "Color": Red, Blue, Green).
  • The Risk: High cardinality. If you have a "Zip Code" feature with 5,000 unique values, OneHot encoding generates 5,000 new columns. This leads to sparse matrices, increased memory usage, and the "curse of dimensionality."

OrdinalEncoder maps each category to an integer (0, 1, 2...).

  • Best for: Ordinal data (e.g., "Size": Small, Medium, Large) or tree-based models like Random Forests or XGBoost, which can handle integer-encoded features effectively.
  • The Risk: Linear models (like Logistic Regression) will interpret the integers as magnitudes, assuming "Large" (2) is twice as "valuable" as "Medium" (1). Only use this for linear models if the categories are truly ordered.

Handling High Cardinality with Target Encoding

When you face high cardinality—features like "User ID" or "City" that have thousands of levels—OneHot encoding is often prohibitive. In these cases, we use Target Encoding (or Mean Encoding).

Target encoding replaces each category with the average target value for that category. If you are predicting churn, the category "New York" becomes the average churn rate of all users in New York.

The Golden Rule: You must compute these averages only on the training set to prevent data leakage. If you calculate the mean target for a category using the entire dataset, the model "sees" the answer for the validation/test rows during training.

Implementation: Target Encoding in a Pipeline

To implement this safely, we use the category_encoders library, which integrates seamlessly with Scikit-Learn pipelines.

PYTHON
from category_encoders import TargetEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestClassifier

# Define our high-cardinality columns
cat_features = [CE9178">'city', CE9178">'zip_code']

# Create the encoder
# min_samples_leaf and smoothing help prevent overfitting
te = TargetEncoder(min_samples_leaf=10, smoothing=10)

preprocessor = ColumnTransformer(
    transformers=[
        (CE9178">'cat', te, cat_features)
    ],
    remainder=CE9178">'passthrough'
)

# Pipeline ensures fit/transform happens only on training folds
pipeline = Pipeline([
    (CE9178">'prep', preprocessor),
    (CE9178">'model', RandomForestClassifier())
])

Hands-on Exercise

  1. Create a synthetic dataset with a high-cardinality feature (e.g., 50 unique cities).
  2. Compare the performance of a LogisticRegression model using OneHotEncoder vs. TargetEncoder.
  3. Observe the memory usage of the resulting feature matrix using .shape. Note how OneHotEncoder expands the feature space significantly.

Common Pitfalls

  • The "Unknown Category" Trap: If your production data contains a category never seen during training, OneHotEncoder will throw an error by default. Set handle_unknown='ignore' in your encoder configuration to prevent deployment crashes.
  • Leakage in Target Encoding: Never perform target encoding before splitting your data. If you calculate the mean target across the whole set, your model will score suspiciously high on local validation but fail in production. Always rely on the Pipeline structure to ensure the mapping is learned solely from the training fold.
  • Overfitting with Target Encoding: If a category has very few samples (e.g., only 1 person in "Small Town"), the target mean will be highly unreliable. Always use smoothing parameters to shrink the estimate toward the global mean.

Recap

Categorical encoding is not one-size-fits-all. Use OneHotEncoder for low-cardinality nominal data, OrdinalEncoder for tree-based models or ordinal data, and TargetEncoder for high-cardinality features. Always encapsulate these transformations within your Pipeline architecture to ensure that your preprocessing logic is applied consistently and that you avoid the silent, deadly trap of data leakage.

Up next: We will discuss how to prune the feature space we’ve just created using Feature Selection in Pipelines.

Similar Posts