2.1.1 · D3Data Preprocessing & Feature Engineering

Worked examples — Types of data (numerical, categorical, ordinal, text)

2,149 words10 min readBack to topic

You have met the four data types in the parent note. Now we do the thing that actually builds skill: we walk through every kind of situation the topic can hand you, one worked example per situation, until no surprise is left.

Before any symbol appears, here is the vocabulary we will lean on, in plain words:

Everything below is built only from these.


The scenario matrix

Every problem this topic throws at you is one of the cells below. Our job is to hit all of them.

# Case class What makes it tricky Example that covers it
C1 Numerical, normal spread baseline scaling Ex 1
C2 Numerical, negative & positive values sign of flips Ex 2
C3 Numerical, degenerate: all values equal , divide-by-zero Ex 3
C4 Numerical, outlier present mean & get dragged Ex 4
C5 Categorical, nominal (no order) one-hot, equal distances Ex 5
C6 Categorical, high cardinality one-hot explodes Ex 6
C7 Ordinal, ordered categories integer codes must respect order Ex 7
C8 Ordinal used in a distance model — the trap uneven gaps assumed uniform Ex 8
C9 Real-world word problem mixing types pick the right tool per column Ex 9
C10 Exam twist: discrete number → categorical? judgement call Ex 10

Numerical cases

The picture below is the mental model for every numerical example: a number line with the mean nailed at the middle, and scaling stretches or squeezes the ruler so one step = one .

Example 1 — C1: ordinary spread

Example 2 — C2: negative and positive raw values

Example 3 — C3: the degenerate case, all values equal

Example 4 — C4: one big outlier


Categorical cases

Picture each category as its own axis. One-hot encoding places every category on a different axis, so no two categories are "closer" than any others — the geometry below.

Example 5 — C5: nominal categories, one-hot

Example 6 — C6: high cardinality explosion


Ordinal cases

Example 7 — C7: ordered categories, integer encoding

Example 8 — C8: the ordinal trap in a distance model


Mixed & twist cases

Example 9 — C9: a full real-world word problem

Example 10 — C10: exam twist — is a discrete count numerical or categorical?


Recall Quick self-test

Scaled value of 22 in [20,24,22,26,28] ::: of a constant column [7,7,7,7] ::: (scaling undefined) Euclidean distance between two distinct one-hot vectors ::: One-hot columns for a 19,000-cardinality pincode ::: 19,000 (use high-cardinality tricks instead) Ordinal code gap Poor→Fair vs Good→Excellent ::: both forced to 1 (uneven reality ignored)

Back to the parent topic.