liveBook · Manning

Categorical encoding

As explained in Grokking Machine Learning

A data-encoding technique that represents each category of a categorical feature with a separate binary indicator feature.

One-hot encoding turns a categorical feature into numerical indicator features, making the categories usable by machine-learning models without assigning them an artificial numeric order. section 13.3 section 13.7

For each categorical feature:

  • Create a separate binary feature for each category. section 9.3 section 13.3
  • Mark whether each example belongs to each category; for a dog, cat, or bird feature, the dog example becomes yes, no, no. section 9.3
  • Prefer these indicators to assigning categories numbers when that would imply an unintended order. section 13.3

Passenger data

Sex and port of embarkation are categorical features in the passenger dataset; one-hot encoding turns them into numerical features for a model. section 13.3

Figure 13.1 One-hot encoding our data to turn it all into numbers for the machine learning model to read. On the left, we have columns with categorical features such as gender or port of embarkment. On the right, we have turned these categorical features into numerical features.

Compare the categorical columns with their numerical indicator features. section 13.3

How Grokking Machine Learning covers it

How deep it goes
Defines one indicator column per category and explains why numeric labels can imply an unwanted order.
Examples
Uses dog, cat, and bird indicators, plus passenger-data features such as sex and embarkation port.
What it assumes
Distinguishes numerical from categorical features using basic examples before describing their conversion.
What you can do after
Apply it with Pandas `get_dummies` to turn categorical columns into numerical indicator columns.
How it connects
Connects categorical encoding to decision trees, linear models, and feature engineering.

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 Machine Learning. The section links point to the parts of the book it draws on.