Encode Categories Without Inventing Order
Names must become numbers carefully
A categorical feature takes values from a set of names or groups, such as browser type. Nominal categories have no meaningful order; ordinal categories do, such as low/medium/high. Encoding nominal values as Chrome=1, Firefox=2, Safari=3 invents numeric distance and order that many models will treat as real.
One-hot encoding creates one binary column per category. Ordinal encoding uses ordered numbers only when that order genuinely exists. High-cardinality features - those with thousands of distinct categories, like product IDs - can explode into thousands of one-hot columns, so practitioners may group rare values, use domain hierarchies, or select models that handle categories appropriately.
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown='ignore')
encoded = enc.fit_transform(train[['browser']])
test_encoded = enc.transform(test[['browser']])
handle_unknown='ignore' prevents a brand-new production category from crashing transformation; it maps that unseen name to all zeros in this encoder. That is operationally safer, but monitoring should still report unseen-category frequency because a surge may signal drift.
Tip: Fit the encoder on training data and reuse it unchanged. Re-fitting separately on test or production data can change column order and silently break the model's feature contract.
Warning: An unknown category is not a garbage bin for upstream corruption. Monitor its rate and retain enough provenance to distinguish a legitimate new category from a broken source value.