Log inStart free
LESSON 4.07 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Categorical encoding

Turn one text column into numbers three ways, and watch a model fit each.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 4.07 · ENCODING BAYSIZE · 4 CATEGORIES · 12 + 4 ROWS
01
Stage 1 of 6 · Text categories
The size is a categorical feature with 4 categories. A linear model needs a number for each one.
Explain it simply

Each row is one parcel: its size and what it cost to ship. Twelve rows train the model, and four test rows check it. A model multiplies inputs by weights, and you can't multiply the word "XL". So each size needs a number first: that's called encoding.

A table of 12 training parcels and 4 test parcels, each with a size and a cost. The chart shows the costs for each size.

category, cost, codetest rowweightpredictionerror
parcelscost $ for each sizesizecost $test1S$42M$83M$64L$95L$96L$87S$68XL$139S$410M$911L$1112M$8t1S$6t2M$9t3L$11t4XL$11$4$6$8$10$12$14$16SMLXLparcelscost $ for each sizesizecost $test1S$42M$83M$64L$95L$96L$87S$68XL$139S$410M$911L$1112M$8t1S$6t2M$9t3L$11t4XL$11$4$6$8$10$12$14$16SMLXL
Toy: 12 training + 4 test parcels · 4 categories · cost = a set mean + seeded noise
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
y^=w x+b,x= ? for c\sy{yh}{out}{\hat y} = \sy{w}{param}{w}\,\sy{x}{data}{x} + \sy{b}{param}{b},\quad \sy{x}{data}{x} = \,?\ \text{for}\ \sy{c}{data}{c}
c∈{S,M,L,XL},k=4, n=12c \in \{\text{S}, \text{M}, \text{L}, \text{XL}\},\quad k = 4,\ n = 12

The model needs a number x for each category c.

Common mix-ups

MYTHOrdinal codes work for any categorical feature.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With city instead of size, does the ordinal line still fit the costs?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.