Data can be in different forms, including categorical data. However, most Machine Learning algorithms only accept numerical data as input. To handle categorical data, we can use functions to transform them into numerical values. One common strategy is one hot encoding, which works well for features with limited categories. However, it can become problematic when dealing with features that have many categories.
To demonstrate, let’s consider the example of one hot encoding. In this case, we have a DataFrame with a categorical feature called “Category”. We can use the pandas library to perform one hot encoding on this feature. The resulting output will be a binary vector where each category is represented by a “True” or “1” value, and all other categories are represented by “False” or “0” values.
However, when the number of categories increases, the one-hot encoded vectors become longer and sparser, which can lead to increased memory usage and computational complexity. For example, if we use the Amazon Employee Access dataset, which contains eight categorical feature columns, one-hot encoding will significantly increase the size of the dataset.
In cases with high cardinality features, target encoding is a better option. Target encoding transforms a categorical feature into a numerical feature without adding any extra columns to the dataset. It works by converting each category into its corresponding expected value, depending on the problem you are trying to solve.
To calculate the expected value for target encoding, we can use the “group_by” function in pandas. This approach considers the conditional probability or average value for each category. However, this simple method may lead to overfitting and can only handle seen categories.
To make target encoding more robust, we can create a custom transformer class and integrate it with scikit-learn. This class inherits from the BaseEstimator and TransformerMixin classes, allowing it to be used in scikit-learn pipelines. The class includes methods for fitting and transforming the data, as well as adding noise to prevent overfitting.
Overall, target encoding provides a more efficient solution for dealing with high cardinality categorical features compared to one hot encoding. It avoids increasing the size of the dataset and provides a numerical representation of the categories without losing important information.
Source link