0
$\begingroup$

I want to ask why Label Encoding before train test split is considered data leakage?

From my point of view, it is not. Because, for example, you encode "good" to 2, "neutral" to 1 and "bad" to 0. It will be same for both train and test sets.

So, why do we have to split first and then do label encoding?

$\endgroup$
5
  • $\begingroup$ Where does this presumption come from? I don‘t see why - as you described the problem - this should lead to leakage. $\endgroup$
    – Peter
    Mar 1, 2022 at 20:09
  • $\begingroup$ Imagine that after the split there is no "good" in the training data. If you had done the encoding after the split, then you would have no idea that there can be a "good". There you have your leakage. $\endgroup$
    – noe
    Mar 1, 2022 at 22:30
  • $\begingroup$ As you mentioned, we have problem when we do encoding after split. So why do you prefer encoding after split? Still didn't get why it is leakage. Can you please give some clarification? Thanks in advance. $\endgroup$
    – Anar
    Mar 2, 2022 at 6:14
  • $\begingroup$ The problem we encounter when doing the split before encoding is just the real world, where we do not have perfect information about the data that our system will be fed in production. That is why we must evaluate our model on unseen data. If you split after encoding, you are evaluating your model under a false premise of knowledge about that very unseen data. $\endgroup$
    – noe
    Mar 2, 2022 at 7:37
  • $\begingroup$ @Anar I condensed my comments as an answer. $\endgroup$
    – noe
    Mar 2, 2022 at 7:39

2 Answers 2

0
$\begingroup$

Imagine that after the split there is no "good" in the training data. If you had done the encoding after the split, then you would have no idea that there can be a "good". There you have your leakage.

Of course, as you mention in the comments, this is a problem. Nevertheless, this problem is just the real world, where we do not have perfect information about the data that our system will be fed in production. That is why we must evaluate our model on unseen data. If you split after encoding, you are evaluating your model under a false premise of knowledge about that very unseen data.

$\endgroup$
0
$\begingroup$

If you perform the encoding before the split, it will lead to data leakage (train-test contamination) In the sense, you will introduce new data (integers of Label Encoders) and use it for your models thus it will affect the end predictions results (good validation scores but poor in deployment).

Suppose test data has new class which was not available in train data but you do label encoding it will be available in the model which leads to data leakage

After the train and validation data category already matched up, you can perform fit_transform on the train data, then only transform for the validation data - based on the encoding maps from train data.

Almost all feature engineering like standarisation, Normalisation etc should be done after train testsplit. Hope it helps

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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