1
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Have you tried modeling with the raw data and not downsampling? $\endgroup$ – Matthew Drury Jul 16 '18 at 19:32
  • $\begingroup$ yes, that gave me rather poor results because it had a rather hard time identifying the rare class. Now what I currently have is I am actually overfitting this variable that I am asking about because I basically used a computed ratio from the entire combined training & testing datasets but of course that is data leakage and not the right way to build a model $\endgroup$ – mathlover Jul 16 '18 at 19:36
  • 3
    $\begingroup$ I suspect that this is enormously relevant: Why is accuracy not the best measure for assessing classification models? $\endgroup$ – Stephan Kolassa Jul 16 '18 at 19:41
  • 2
    $\begingroup$ Once you had fit the probability model, how did you use it to assign classes? It's common to see people use a threshold of 0.5, under some belief that it is optimal, but this is ALWAYS a bad idea. You need to tune your classification threshold in all cases. $\endgroup$ – Matthew Drury Jul 16 '18 at 19:44
  • 2
    $\begingroup$ @mkheifetz don't get fooled by the title of that question, the advice in Stephan's answer is much, much more general. Please read it carefully. $\endgroup$ – Matthew Drury Jul 16 '18 at 19:45
1
$\begingroup$

I hope you have followed the good advice in the comments. Why do you use downsampling? It is most often used to solve a nonproblem. See Why downsample? and many of its answers. With only 400k rows memory shouldn't be a problem, if it is, get some better software. Their problem may be the use of accuracy, which is an improper score function, see Is accuracy an improper scoring rule in a binary classification setting?.

Then the question about target (or mean) encoding. That is an idea from machine learning used with categorical variables with very many levels. Your variable Industry probably does not have that many levels, so you could try other ideas, like dummy variables with regularization, maybe try glmnet. Glmnet uses sparse matrices so many levels isn't a big problem. If there is many levels, see some of the ideas here: Principled way of collapsing categorical variables with many levels?.

Finally, if you still go for target encoding, see my answer here: Strange encoding for categorical features

$\endgroup$
  • 1
    $\begingroup$ As others have said, any time you entertain downsampling you are fundamentally not understanding the true problem. $\endgroup$ – Frank Harrell Jun 29 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.