In the Replacing categories with ordinal numbers recipe, we replaced categories with integers, which were assigned arbitrarily. This encoding works well with non-linear machine learning algorithms that can navigate through the arbitrarily assigned digits to try and find patterns that relate them to the target. However, this encoding method may not work so well with linear models.
We can instead assign integers to the categories given the target values. To do this, we do the following:
- Calculate the mean value of the target per category.
- Order the categories from the one with the lowest to the one with the highest target mean value.
- Assign digits to the ordered categories, starting with 0 to the first category all of the way up to k-1 to the last category, where k is the number of distinct categories.
This encoding technique creates...