liveBook · Manning

Ordinal data

As explained in Ensemble Methods for Machine Learning

An ordinal feature is a categorical input variable whose categories have an inherent ordering.

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.
CH08_F01_Kunapuli

Compare nominal color, ordinal size, and continuous cost in one visual example. section 8.1.1

How Ensemble Methods for Machine Learning covers it

How deep it goes
Explains ordinal-versus-nominal intuition, numeric mappings, scikit-learn usage, and one-hot tradeoffs without covering model-specific effects.
Examples
Uses T-shirt sizes S–XL, colors, predicted cost, explicit category lists, and encoded feature matrices.
What it assumes
Assumes basic machine-learning feature terminology while defining categorical, nominal, and ordinal variables through examples.
What you can do after
Apply ordinal and one-hot encoders while matching the representation to each feature’s category structure.
How it connects
Connects categorical features, nominal data, ordinal encoding, one-hot encoding, target statistics, and machine-learning preprocessing.

Also explained in

Compare how 8 books explain ordinal data →

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.