Categorical data contains values that identify membership in discrete named or ordered groups rather than measuring a continuous amount. Diamond Cut, Color, and Clarity are categorical features; Price and Depth are continuous values. section 8.15
Categorical data commonly appears in classification, where a model predicts a discrete class such as car or truck, bird or not bird, or tablet, laptop, or phone. Because models work with numbers, categories must be represented carefully. section 8.15 section 8.9
Choose a representation that matches the meaning of the categories:
- Use one-hot encoding to create a separate indicator feature for each category. For Cut, each row receives a 1 in its category column and 0 in the others. section 8.9
- Use label encoding only when the categories express a meaningful rating or order, such as superior Cut, Clarity, or Color. Otherwise, numeric labels can make a model infer an unintended ranking. section 8.9
- Handle missing categorical values separately: the mode, or most frequent category, can fill gaps, while deleting rows or estimating values may also be appropriate. section 8.8 section 8.9
Encoding diamond origin
Suppose a diamond dataset contains an Origin value identifying where each diamond was sourced. Encoding locations as 0, 1, and 2 would falsely suggest that one location is greater than another. One-hot encoding instead gives each location its own column, avoiding that artificial ranking. If Origin is missing, remove the row or estimate the value when the feature matters. 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 diamond rows as one-hot-encoded Cut columns, where each category is represented by a separate 0-or-1 feature. section 8.9















