2.1.6 · D1Data Preprocessing & Feature Engineering

Foundations — One-hot encoding and label encoding

1,966 words9 min readBack to topic

Before you can judge why one encoding is right and another is wrong, you need to be fluent in the little symbols the parent note throws at you: sets, subscripts, vectors, distance, the dot product, the Kronecker delta, and matrix rank. This page builds every one of them from zero, in the order they depend on each other.


1. Category, and the set that holds them

Read that out loud: "the collection containing , , and so on up to ."

  • The letter (for category) is a placeholder — we don't know the actual word, so we use a letter.
  • The little number below, called a subscript, is a name tag that tells apart the first, second, third item. = "first category", = "second category".
  • The letter is the count of how many categories exist. If we have {Red, Blue, Green}, then .
Figure — One-hot encoding and label encoding

Look at the figure: three unordered labels floating in a bag. Nothing says one comes before another. Our job is to give each a number-address while keeping that fairness.


2. A mapping: turning a label into a number

The bendy arrow ("maps to") is different from the straight arrow: it points at what one input becomes. So means "the rule turns Red into 0".


3. Zero-indexing and the range of integers

The parent writes .

  • The symbol means "is a member of". Picture an item sitting inside the bag. So says " is one of 0, 1, or 2".
  • Zero-indexing means we start counting at 0, not 1. So categories get the addresses . For : the addresses are — still three of them, just shifted to start at zero (a programming convention).

4. Order symbols: and

The parent uses two "less-than" signs and they mean different things.

Figure — One-hot encoding and label encoding

The figure shows two number lines. Top (ordinal): shirt sizes land on 0,1,2,3 and the arrow of increasing number matches increasing real size — the order is earned. Bottom (nominal): countries dumped onto 0,1,2 — the arrow now lies, claiming Brazil is "twice" India.


5. Vectors and the arrow

One-hot encoding stops using a single number and starts using a row of numbers.

Figure — One-hot encoding and label encoding

In the figure, three arrows point straight out along the three axes of a 3-D room. Each is a "one-hot" colour. Notice they are all the same length and sit at the same angle to each other — no colour is favoured. That symmetry is the whole reason one-hot exists.


6. Distance: how far apart two categories land


7. The dot product and the Kronecker delta

Now the parent's line reads in plain words: "a one-hot arrow dotted with itself gives 1 (they fully agree), and dotted with a different one gives 0 (perfectly perpendicular)." Perpendicular arrows are the geometric picture of "totally independent, no accidental relationship."


8. Matrix, design matrix , and rank

The dummy-variable-trap section needs three last words.


Prerequisite map

Category and Set

Mapping f

Integers and zero-indexing

Order symbols < and prec

Label Encoding

Vector in R to the n

Standard basis e_i

Distance length

Dot product and delta

One-hot Encoding

Design matrix and rank

Dummy variable trap

Encoding choice

Notice how the left branch (order symbols) feeds label encoding, while the right branch (vectors, distance, dot product) feeds one-hot. They meet at the final decision. Deeper consequences flow onward into Curse of Dimensionality (too many one-hot columns), Decision Trees (which handle label codes more gracefully), Target Encoding (a smarter alternative), and the broader craft of Feature Engineering.


Equipment checklist

Cover the answer; can you say each in one breath?

What does mean and what is ?
A set of distinct categories; is how many there are.
What is the difference between and ?
compares plain numbers; means "comes before in a real-world order" (like S before M).
What does tell you about addresses?
Zero-indexed integer labels; largest is , not .
What is and what does it look like?
The standard basis vector — 1 in slot , 0 elsewhere; an arrow along one axis. It IS a one-hot code.
What is ?
The space of vectors with real-number coordinates; is the room around you.
Compute for and say why it matters.
; all one-hot categories are equidistant, so none is falsely "closer".
What does say in words?
One-hot arrows agree fully with themselves (1) and are perpendicular to others (0) — geometrically independent.
Why does full one-hot break the design matrix ?
Its columns sum to 1 (redundant), dropping rank below , so regression has no unique solution — hence drop one column.