liveBook · Manning

Label encoding

As explained in Grokking AI Algorithms, Second Edition

Label encoding maps each category to a numerical label, typically an integer in a fixed range.

Label encoding replaces each category with a number. It lets a model receive numeric input instead of strings such as “Fair” or “Good.” The choice of numbers matters: a model may treat a larger label as carrying greater weight. section 8.9

Encoding a diamond rating

A possible encoding for Cut is Fair → 0, Good → 1, Very Good → 2, Premium → 3, and Ideal → 4. These labels follow a rating order. By contrast, assigning numbers to a diamond’s Origin would suggest that one source location ranks above another, even though none does in this dataset. section 8.9

Use label encoding for ratings or rating-related categories. For categories with no meaningful ranking, use one-hot encoding instead: it gives each category its own indicator rather than a potentially misleading number. 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

The Cut columns show the one-hot alternative: each diamond has a 1 under its category and 0 under the others. section 8.9

A related approach, scalar encoding, assigns values that express relative impact—for example, positive for a goal and negative for a collision. It is compact, but can mislead a neural network if the assumed relationships between categories do not hold. section 10.11

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines numeric labels and explains when to choose them over one-hot encoding, without giving an implementation workflow.
Examples
Contrasts diamond Cut ratings with unranked Origin values and compares them with reward-valued neural-network inputs.
What it assumes
Introduces the need to convert string categories into numbers without assuming prior knowledge of encoding methods.
What you can do after
Practice/perform categorical data preparation by labeling ratings and choosing one-hot encoding for categories without a ranking.
How it connects
Connects categorical data preparation to one-hot encoding, model bias, and scalar encoding for neural-network inputs.

Also explained in

Compare how 4 books explain label 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.