liveBook · Manning

Categorical encoding

As explained in Grokking AI Algorithms, Second Edition

One-hot encoding represents each category by a separate binary indicator, with one indicator active for each example.

A model needs numbers, but a category such as a diamond’s cut or its place of origin may be written as a word. One-hot encoding turns each possible category into its own 0-or-1 indicator. Picture a row of switches: exactly one is on for each example. section 8.9

Encode a diamond’s cut

Make five columns: Fair, Good, Very Good, Premium, and Ideal. For a Good cut, put 1 in Good and 0 in the other four. Each diamond gets one active cut column. section 8.9

Table 8.6 The diamond dataset with one-hot-encoded values

Carat

Cut: Fair

Cut: Good

Cut: Very Good

Cut: Premium

Cut: Ideal

1

0.30

0

1

0

0

0

2

0.41

0

0

0

0

1

3

0.75

0

0

1

0

0

4

0.91

0

0

0

0

0

5

1.20

1

0

0

0

0

6

1.21

0

1

0

0

0

7

1.31

0

0

0

1

0

8

1.50

0

0

0

1

0

9

1.74

0

0

1

0

0

10

1.83

1

0

0

0

0

11

1.96

1

0

0

0

0

12

1.19

0

0

0

1

0

Compare the Cut columns across diamond rows to see how the active indicator moves with the category. section 8.9

This is especially useful when categories have no meaningful order. Numbering diamond origins could suggest that one location ranks above another; separate indicators avoid that suggestion. Label encoding is better reserved for ratings where the numbers carry meaningful rank. section 8.9

Encode nearby block states

An artificial neural network can represent one block as [1, 0, 0, 0] for empty or [0, 0, 1, 0] for pedestrian. Four indicators for each of eight adjacent blocks produce 32 inputs. The larger input lets the network learn different effects for distinct states without treating them as a numeric scale. section 10.11

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines binary indicators and works through category-to-input examples, but provides no encoding code.
Examples
Illustrates five diamond Cut indicators and four block-state indicators repeated across eight adjacent blocks.
What it assumes
Introduces numeric representation of categorical strings from the start; ANN use assumes familiarity with input nodes.
What you can do after
Apply it to encode unordered categories and determine the corresponding number of ANN inputs.
How it connects
Connects one-hot encoding to label encoding, data preparation, and artificial neural networks.

Also explained in

Compare how 13 books explain categorical encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.