← capítulo

Turning data into features

A model never sees your data — it sees a matrix of numbers.

How a column becomes those numbers is a modeling decision, not bookkeeping.

The four kinds of column

The type of the column decides how you're allowed to encode it.

The trap

Write a name down as a number and you invent an order it never had.

Label-encode color as blue = 0, green = 1, red = 2 and you've told the model:

None of that is true of colors. A linear model believes all of it.

One-hot refuses to invent the order

onehot(x)=(1[x=c1], 1[x=c2], , 1[x=cK])\operatorname{onehot}(x) = \big(\mathbb{1}[x = c_1],\ \mathbb{1}[x = c_2],\ \dots,\ \mathbb{1}[x = c_K]\big)

One column per category, exactly one lit per row — three orthogonal axes, nothing closer to anything.

Why the integer lies

A linear model's contribution from a label integer is one weight times the code:

η=wcode(x)+b\eta = w \cdot \operatorname{code}(x) + b

Its response must be monotone in the code — it cannot make the middle category an outlier. And any distance reads

code(ci)code(cj)\big\lvert \operatorname{code}(c_i) - \operatorname{code}(c_j) \big\rvert

so 0 and 2 are twice as far apart as 0 and 1. Fine for real orders, a fabrication otherwise.

Watch the fake order dissolve

Color alone, fit two ways. Rings = true rates; diamonds = the model. Green's true rate is 0.07.

Label-encoded, green is dragged to 0.52. One-hot, it drops to 0.10. Accuracy 0.52 → 0.79.

What it costs the model

Same table, one split, four runs. Test accuracy — fraction of held-out visitors called right (unitless, 0–1).

Linear model: 0.573 → 0.760, a 0.187 lift. Tree: 0.747 → 0.733, barely a flinch.

Takeaways