liveBook · Manning

Label encoding

As explained in Ensemble Methods for Machine Learning

Ordinal encoding maps each category of a categorical variable to a single numeric value, optionally preserving an inherent category order.

Label encoding, also called ordinal encoding, converts each categorical value into one integer so machine-learning models can consume it as a numeric feature. It is unsupervised: the mapping uses feature values, not target labels. section 8.1 section 8.1.2

Build an encoding by following these decisions:

  • List the allowed categories and assign each a distinct integer, such as red → 0, blue → 1, and green → 2. section 8.1.2
  • Preserve meaningful order when the feature is ordinal: S, M, L, XL can become 0, 1, 2, 3, or the reverse. section 8.1.2
  • Treat nominal mappings cautiously: assigning integers creates an apparent ordering even when categories have no relationship. section 8.1.2 section 8.5
  • Fit preprocessing on training data and apply the learned transformation consistently to later data. section 8.1.4

Encoding color and size

Given rows such as [red, M] and [blue, XL], specify color as [red, yellow, green, blue] and size as [XL, L, M, S]. scikit-learn's OrdinalEncoder then produces [0, 2] for [red, M] and [3, 0] for [blue, XL]. Unobserved categories, such as yellow, still receive their specified value.

Label encoding is compact because one feature remains one numeric column, but it can distort relationships for nominal variables. Compare it with one-hot encoding and evaluate the complete preprocessing-model combination using suitable metrics, especially when classes are imbalanced. section 8.1.2 section 8.3.3

How Ensemble Methods for Machine Learning covers it

How deep it goes
Defines mappings, demonstrates explicit category orders, and stops short of detailed encoding internals.
Examples
Uses color and size mappings, an explicit OrdinalEncoder configuration, encoded arrays, and ensemble comparisons.
What it assumes
Assumes categorical variables and machine-learning preprocessing, while distinguishing nominal from ordered categories.
What you can do after
Apply it via scikit-learn's OrdinalEncoder and compare its model performance against alternative encodings.
How it connects
Connects categorical features with feature engineering, one-hot encoding, target encoding, and target leakage.

Also explained in

Compare how 4 books explain label 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.