liveBook · Manning

Categorical encoding

As explained in Ensemble Methods for Machine Learning

Categorical encoding is the transformation of categorical feature values into numerical representations suitable for machine-learning models.

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

How Ensemble Methods for Machine Learning covers it

How deep it goes
Explains ordinal and one-hot representations, demonstrates encoded matrices, and connects them to reusable preprocessing pipelines.
Examples
Uses color, size, Adult income data, missing workclass values, and scikit-learn encoded matrices.
What it assumes
Assumes familiarity with features, labels, training data, and machine-learning models; defines categorical features and encoding before implementation.
What you can do after
Apply encoding via scikit-learn pipelines and compare encoders within ensemble-model evaluation.
How it connects
Connects feature engineering with ordinal encoding, one-hot encoding, imputation, preprocessing pipelines, target encoding, and ensemble learning.

Also explained in

Compare how 13 books explain categorical encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Ensemble Methods for Machine Learning. The section links point to the parts of the book it draws on.