How it works...
In this recipe, we replaced each category in a categorical variable with an integer, assigned arbitrarily, using pandas, scikit-learn, or Feature-engine.
We loaded the dataset and divided it into train and test sets as described in the previous recipe. We worked first with the categorical A7 variable. With pandas unique(), we displayed the unique values of A7 and using Python's list comprehension syntax, we created a dictionary of key-value pairs, where each key was one of the unique categories, and each value was a digit that would replace the category. Finally, we used pandas' map() method to replace the strings in A7 with the integers indicated in the dictionary.
To carry out ordinal encoding with scikit-learn, we used ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access