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



