I have a categorical variable, Industry, that has different values in a dataset that is over 400K datapoints. This dataset is highly imbalanced, the ratio of roughly 99/1. What I am doing is significantly undersampling the majority class to create a 50/50 dataset of 8000 datapoints (I definitely lost most of the training points because I need most of those to have a prediction made on them, though those are mainly from the majority class so I am not losing too many rare class data).
What I would like to do is mean encode this variable, Industry, against the target. For example, let's say I have NYC appear 1000 times in the training data, and 100 of them are in the positive class the value for datapoints with NYC will be 100/1000=.1 will be the value of this new feature, etc.
The problem is that because I am undersampling to make my dataset balanced, that skews this ratio by creating fake good ratios on the minority class because it now represents 50% of my training data rather then 1%. Hence, my training column will significantly overvalue the ratio compared to the testing set and will not help my machine learning algorithms at all.
What should I do in this case to create a good mean encoded ratio?