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



