Mean encoding or target encoding implies replacing the categories with the average target value for that category. For example, if we have a City variable, with the categories of London, Manchester, and Bristol, and we want to predict the default rate; if the default rate for London is 30%, we replace London with 0.3; if the default rate for Manchester is 20%, we replace Manchester with 0.2; and so on. The same can be done with a continuous target.
As with any machine learning algorithm, the parameters for target encoding, that is, the mean target value per category, need to be learned from the train set only and used to replace categories in the train and test sets.
In this recipe, we will perform mean encoding using pandas and Feature-engine.