2.1.7 · D1Data Preprocessing & Feature Engineering

Foundations — Ordinal and target encoding

3,103 words14 min readBack to topic

Before you can read the parent note, you must be able to read its symbols. This page builds every one of them from nothing — no algebra assumed. We go slowly, in the order they depend on each other, so that by the last section every letter — including the smoothed encoding the parent calls — already means something to you.


0. What is a "category" and a "feature"?

Picture a spreadsheet. Each row is one example (one house, one student). Each column is one feature. A categorical column stores words.

Figure — Ordinal and target encoding
Figure s01. A data table. The yellow header names each column (feature). The blue column holds words — a categorical feature. Reading down that blue column, notice NYC appears more than once: rows repeat, but distinct labels are few. That gap between "how many rows" and "how many distinct labels" is the theme of the next two sections.

Why not just number them randomly (NYC=1, Seattle=2, Portland=3)? Because the model would then believe Portland − NYC = 2 steps apart, an ordering you never intended. Encoding is only correct when the numbers mean something. That is what ordinal and target encoding each guarantee, in two different ways.


1. The set:

The three dots mean "and so on in the same pattern". So = "the first category, the second, ..., up to the -th".

The word cardinality just means "the value of " — the number of distinct categories. "High-cardinality" (the parent's motivation for target encoding) simply means is large, e.g. 500 cities. Keep that word — it decides which encoding you pick. See Curse of Dimensionality for why large hurts.


2. The subscript index and the samples

and are different counts:

  • = how many rows total (could be millions).
  • = how many distinct labels appear (could be 4, or 500).
Repeat-question
In [NYC, NYC, Seattle], what are and ?
Answer
rows, distinct labels.

3. The mapping function and the arrow

Think of a function as a lookup dictionary: you hand it a word, it hands back a number. is the ordinal dictionary; is the target dictionary. The subscript on just tells you which dictionary.

Figure — Ordinal and target encoding
Figure s02. The mapping drawn as arrows. Each ordered word on the left (blue) points to one number on the right (yellow). Crucially the words are stacked in their real-world order S, M, L, XL — so the arrows land on 1, 2, 3, 4 in the same order. If you shuffled the left column the arrows would still work as a dictionary, but the meaning (order) would be lost. That ordering choice is the subject of the next callout.

Read aloud: "the ordinal code of the -th category is its position number ." So the smallest category gets 1, the next gets 2, and so on. That is the whole formula — the parent's fancy line is just this dictionary written compactly.


4. The condition line — "the rows where..."

This is the single scariest-looking symbol in the parent note. Let us disarm it.

Figure — Ordinal and target encoding
Figure s03. The filter in action on a city column. Every row is listed; the pink boxes are the ones surviving the test — rows 1, 2, 4. Only those pink rows' targets (500, 600, 550) get added and averaged. The greyed-out Seattle and Portland rows are simply invisible to this particular category's calculation. Change to Seattle and a different set of rows lights up.

Why the topic needs it: target encoding must gather only the rows of one category to average their outcomes. This filter is how the maths says "look at just the NYC houses."


5. Sum , count , the mean, and the target

Now the mean falls out naturally. A mean (average) is "total divided by count":

The bar over (read "y-bar") is standard shorthand for "the mean of". The little "" means "add through , every row". The difference between the two formulas is which rows you include: filters to one category, takes them all.

Now we can finally fill in the second dictionary from §3 — the one we named but left blank.


6. The weighted average and the smoothing ,

The last symbols appear in smoothed target encoding. They are all built from means you now understand.

Figure — Ordinal and target encoding
Figure s04. The slider. Blue pin = category mean (300 for lonely Portland); yellow pin = global mean (475). The pink dot is where a category with lands — flung almost onto the global pin because we barely trust one observation. The blue square shows a category with landing right next to its own mean. The dot's position is the weight : near the yellow pin means near 0, near the blue pin means near 1.

Cover-all-cases table:

situation encoding leans toward
unseen at test time global mean (exactly)
lonely category global mean (safe)
medium category still mostly global
big category its own mean (trusted)
(no smoothing) any raw mean — back to unsmoothed

Prerequisite map

Category and feature

Set c1..ck and cardinality k

Samples x1..xn and count n

Mapping function f and arrow

Ordinal encoding f = rank

Filter rows where xi equals c

Sum sigma and count nc

Category mean and global mean

Target function f_target

Weighted average with w

Smoothing m and TE smooth

Ordinal and target encoding

Related vault stops once you are through here: One-hot Encoding (the wasteful alternative for high ), Feature Scaling (what to do with the numbers afterward), Cross-Validation (how to avoid target leakage), Tree-based Models and Overfitting (why smoothing matters).


Equipment checklist

Say the answer aloud before revealing.

I can state the difference between and
= number of rows; = number of distinct categories.
I know what means
the -th category (a label), picked out by its position number .
I know where the ordinal order comes from
from you — a domain ranking (or target-driven), never invented by the computer; alphabetical is a trap.
I can read
"the set of row-numbers where the label equals " — a filter keeping only those rows.
I know what does
adds up all the listed items.
I can define
the count of how many rows carry category .
I can compute
sum of over category- rows, divided by (the mean target for that category).
I can write as a formula
— the mean of every row's target.
I can give the formal spec of
— map each category to its category mean.
I know what and mean
= long-run average (expectation); = random variable for the category of a random row.
I know how to handle
fall back to the global mean (or use smoothing, which returns the global mean automatically).
I can explain in words
replace each ordered category with its rank number, preserving order.
I know what the weight controls
how much to trust the category's own mean vs. the global mean; between 0 and 1.
I can derive
it is the fraction of real rows in a pool of real plus imaginary anchor rows (Bayesian pseudo-counts).
I can convert the slider form to the fraction
substitute , share the denominator, combine numerators into .
I know what is
the smoothing parameter — a count of imaginary rows sitting at the global mean.
I can predict the effect of large
stronger pull of every category toward the global mean (more skepticism).