2.1.6 · D3Data Preprocessing & Feature Engineering

Worked examples — One-hot encoding and label encoding

2,331 words11 min readBack to topic

This page is the practice arena for the parent topic. The parent told you what label and one-hot encoding are. Here we grind through every kind of situation a real dataset can throw at you — ordered data, unordered data, a category that never appears, a category the model has never seen, the trap of too many columns, and an exam-style twist.

Before we start, one promise: every symbol used below was defined in the parent, but we will re-anchor each one to a picture the first time it appears here so you never have to scroll back.


The scenario matrix

Think of encoding as a machine: you feed it a column of categories, it hands back numbers. The behaviour of that machine changes depending on the kind of input. This table lists every kind of input we must survive.

Cell Scenario Key question it tests Right tool
A Ordinal, clean data Does order survive? Label encoding
B Nominal, few categories Kill the fake order One-hot
C Nominal, high cardinality Too many columns → Curse of Dimensionality One-hot vs alternatives
D Linear model, all columns kept The dummy-variable trap Drop-one
E Unseen category at prediction time Robustness to new data handle_unknown
F A category that appears zero times Degenerate / empty column Column pruning
G Decision Trees vs Linear Regression Does the model even care? Tool depends on model
H Exam twist: interpret a fitted coefficient Reference level meaning Drop-one + intercept

We now walk one worked example per cell (a couple share a figure). Each begins with a Forecast — pause and guess the answer before reading the steps.

Figure — One-hot encoding and label encoding

Look at the picture: on the left number line, Red–Blue–Green sit at — Blue is trapped between Red and Green, and Green is twice as far from Red as Blue is. On the right cube, each category is a corner; the three edges drawn are all the same length. That equal length is the the parent proved. Everything below is a consequence of choosing the left picture or the right one.


Cell A — Ordinal data, done right


Cell B — Nominal, few categories


Cell C — High cardinality (the column explosion)


Cell D — The dummy-variable trap in a linear model


Cell E — An unseen category at prediction time

Figure — One-hot encoding and label encoding

The figure shows the origin (the unknown/all-zero point, plum) sitting equidistant from all three category corners — the geometric reason handle_unknown="ignore" is a fair default.


Cell F — A category that appears zero times


Cell G — Does the model even care? Trees vs linear


Cell H — Exam twist: interpret a fitted coefficient


Recall

Recall Why is one-hot equidistant but label encoding is not?

One-hot puts each category on a distinct axis, so every pair is apart ::: label encoding lines them on a number line, so distances (and order) are baked in unequally.

Recall What is the reference level in drop-one encoding?

The dropped category ::: its effect is absorbed into the intercept, and every remaining coefficient is a difference from it.

Recall Encoded value of an unseen category with handle_unknown="ignore"?

The all-zeros vector ::: it sits at the origin, equidistant (distance 1) from every known category corner.

Recall Why can label encoding be acceptable for trees but not linear regression?

Trees split on order via thresholds and ignore distance ::: linear regression multiplies the code by a weight, so fake numeric spacing forces spurious proportional effects.