Sub topic 2
Nominal and ordinal categories
This chapter assumes you can work with pandas DataFrames and know that a feature matrix holds one row per sample and one column per feature. Before choosing an encoding, you need to know what kind of categorical feature you have. The two kinds are nominal and ordinal, and they call for different treatment.
Nominal categories
A nominal feature has labels with no natural order. Examples are color (red, green, blue), city (Paris, Berlin, Rome) and payment_method (card, cash, transfer). You can't say that Paris is "greater than" Berlin in any way that means something for your model. Any ordering you impose, such as alphabetical order, is an accident of how the labels are written.
Ordinal categories
An ordinal feature has labels with a meaningful order, but the gaps between labels are not defined. Examples are size (small, medium, large), satisfaction (poor, fair, good, excellent) and education (high school, bachelor, master, doctorate). You know that medium is bigger than small. You don't know whether the step from small to medium equals the step from medium to large.
Telling them apart
Ask whether it makes sense to sort the categories and whether the sorted order would still make sense if a colleague sorted them independently. If both answers are yes, the feature is ordinal. If the only available order is alphabetical, it is nominal.
Recording order explicitly
pandas can store the order, so you don't leave it to chance. In this example you declare the order of size yourself:
import pandas as pd
sizes = pd.Series(["small", "large", "medium", "small"])
ordered = pd.Categorical(
sizes,
categories=["small", "medium", "large"],
ordered=True,
)
print(ordered.codes)
print(ordered < "large")The codes are [0 2 1 0]. Their order follows the order you declared, not the alphabet. Without the categories argument, pandas would sort the labels alphabetically (large, medium, small) and the codes would be reversed from what you intend. The comparison ordered < "large" works only because the categorical is ordered.
Why the distinction matters for encoding
The kind of feature decides which encodings are safe:
| Feature type | Example | Integer codes | One-hot encoding |
|---|---|---|---|
| Nominal | `color` | Misleading, because they invent an order | Default choice |
| Ordinal | `size` | Reasonable if you declare the order | Valid, but discards the order |
| Ordinal with few levels | `size` | Often fine for tree models | Useful for linear models that should not assume equal steps |
Integer codes for an ordinal feature assume equal spacing. A linear model treats small, medium and large as 0, 1 and 2, so the effect of going from small to medium is forced to equal the effect of going from medium to large. If that assumption is wrong, one-hot encoding lets the model learn a separate effect for each level, at the cost of losing the ordering information.
Some features sit in between. A rating from 1 to 5 is numeric, but the steps may not be equal. Treat it as ordinal and test both approaches.
Don't assume that stored data tells you the type. A column of integers such as zip_code or department_id looks numeric but is nominal. A column of strings such as "low", "medium" and "high" looks nominal but is ordinal.
The next chapter shows what goes wrong when you encode a nominal feature with arbitrary integers.
Exercise
A DataFrame has the columns shirt_size (S, M, L, XL), country (FR, DE, IT) and store_id (stored as the integers 101, 102, 205). Which are nominal and which are ordinal? Which is easiest to misclassify?
Show answer
shirt_size is ordinal because S < M < L < XL. country is nominal. store_id is nominal even though it is stored as integers, and it is the easiest to misclassify because the numbers suggest an order or a magnitude that doesn't exist.