Machine-learning models usually require numeric inputs, but features such as color, size, state, or ZIP code contain discrete nonnumeric categories. Categorical encoding transforms those values into numerical features while preserving the relationships that actually exist. Because encoding is feature engineering, the choice affects model performance and interpretability. section 8.0 section 8.1.2
Choose the representation according to whether the categories have a meaningful order:
- Use ordinal encoding when categories are genuinely ordered. For example, S, M, L, and XL can receive values that preserve increasing or decreasing size. section 8.1.2
- Use one-hot encoding for nominal categories such as color. It creates a separate indicator feature for each category, so red, green, and blue do not acquire an artificial ranking. section 8.1.2
- Treat missing categorical values consistently before encoding. A preprocessing pipeline can replace them with a distinct category such as missing. section 8.3.2
Encoding color and size
Given rows containing color and size, OneHotEncoder can use the categories red, green, blue and XL, L, M, S. A row such as red, M becomes seven binary values: one active color position, one active size position, and zeros elsewhere. The resulting matrix represents both features numerically without implying that red is less or greater than blue.
For production modeling, place imputation and encoding inside a ColumnTransformer and Pipeline. Applying the same fitted preprocessing strategy to training and test data keeps transformations consistent and minimizes data leakage. Encoders can then be compared alongside ensembles rather than treated as an isolated preprocessing decision. section 8.3.2












