Ordinal data is categorical data whose values have a meaningful order. T-shirt size is a simple example: S < M < L < XL. Color is categorical too, but red, blue, and green have no inherent ranking, so color is nominal rather than ordinal. Identifying this distinction guides encoding choices and helps preserve the relationships represented by the data. section 8.1.1
T-shirt sizes
Suppose a model predicts T-shirt cost from color and size. Encoding size as S=0, M=1, L=2, XL=3 preserves its increasing order; reversing the scale also preserves the relationship. Encoding color with numbers does not create a meaningful ranking, because any permutation of its categories is equally valid. section 8.1.1 section 8.1.2
Use the category structure to choose an encoding:
- Apply ordinal encoding when integer values should preserve an existing category order. section 8.1.2
- Avoid treating arbitrary integer assignments for nominal categories as meaningful, because ordinal encoding imposes an ordering. section 8.1.2
- Use one-hot encoding when categories should not be ordered; it creates binary columns for each category but can become inefficient with many categories. section 8.1.2 section 8.5
In scikit-learn, OrdinalEncoder can receive explicit category orders and transform categorical columns into numeric values. For example, specifying ['XL', 'L', 'M', 'S'] maps those sizes to 0, 1, 2, and 3. The encoder can also include a category not observed in the current rows, such as yellow in a color specification. section 8.1.2
Figure 8.1 T-shirts in this example data set are described using two categorical features: color and size. Categorical features can be either (1) nominal, where there is no ordering between the various categories, or (2) ordinal, where there is an ordering between the categories. The third feature in this data set, cost, is a continuous, numeric variable.
Compare nominal color, ordinal size, and continuous cost in one visual example. section 8.1.1







