liveBook · Manning

One-hot encoding

As explained in Ensemble Methods for Machine Learning

One-hot vectorization represents each category of a categorical variable as a separate binary indicator component.

One-hot encoding turns each category into its own binary indicator. A row activates the indicator for its observed category, avoiding the artificial ordering imposed by ordinal encoding. section 8.1.2

Use one-hot encoding as an unsupervised feature-engineering step before model training:

  • Choose the categorical features and specify their possible categories. section 8.1.2
  • Create one binary column for each category, then concatenate the resulting indicators across features. section 8.1.2
  • Fit the transformation using features rather than targets, keeping it separate from supervised target-statistics encoding. section 8.1

Color and size

For rows containing color and size, scikit-learn's OneHotEncoder produces seven indicators when color has three categories and size has four. A row such as red and M activates the red and M positions while leaving the others at zero. section 8.1.2

import numpy as np
X = np.array([['red', 'M'],
             ['green', 'L'],
             ['red', 'S'],
             ['blue', 'XL'],
             ['blue', 'S'],
             ['green', 'XL'],
             ['blue', 'M'],
             ['red', 'L']])

Inspect the small color-and-size input before comparing it with the one-hot matrix produced by the encoder. section 8.1.2

Encoding choice can affect model performance and interpretability. Across the reported random-forest, LightGBM, and XGBoost comparisons, one-hot encoding provides competitive balanced-accuracy and F1 results, though its relative advantage depends on the ensemble. section 8.3.3

How Ensemble Methods for Machine Learning covers it

How deep it goes
Explains the binary-indicator transformation and demonstrates scikit-learn output, but does not develop implementation internals.
Examples
Uses color and size categories, a small encoded array, and ensemble evaluations with random forest, LightGBM, and XGBoost.
What it assumes
Assumes categorical features and model training, while reinforcing why nominal categories should not receive arbitrary numeric ordering.
What you can do after
Apply it via scikit-learn's OneHotEncoder and compare its model performance using accuracy, balanced accuracy, and F1.
How it connects
Contrasts with ordinal encoding and target encoding, then connects encoding choices to random forests, LightGBM, XGBoost, and target leakage.

Also explained in

Compare how 6 books explain one-hot 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.