0
$\begingroup$

There are many steps when building a machine learning model, such as:

  • Dealing with missing data;
  • Converting categorical features into dummies (or other type of encoding);
  • Splitting into train and test;
  • Applying StandardScale (or other type of scaling/normalization).

What is the correct order?

Additionally, if I have missing in the test features, should I apply the same mean/median (or other missing imputation) used in the training data? I mean, if the missing column in training data has mean of 5, should I imput 5 in the test data?

I read this question: https://datascience.stackexchange.com/questions/53138/which-comes-first-multiple-imputation-splitting-into-train-test-or-standardiz, but it is not complete. Also, it is not the same order as this post is https://towardsdatascience.com/logistic-regression-a-simplified-approach-using-python-c4bc81a87c31, and not sure if is the same of this one https://towardsdatascience.com/handling-missing-data-for-a-beginner-6d6f5ea53436

$\endgroup$
0
0
$\begingroup$

In general I would say:

  • Split Train-Test
  • Deal with missing data
  • Convert categorical
  • Normalize numerical

In general it is a good idea to do the train split early, so that you avoid having some data leakage due to any of the other things that you do. If you deal with missing data by using the mean value of the column for example, you should take the mean of the training set and use that for the testing set. If you do this before the splitting, you would be leaking information about the testing data into your training. Same thing goes for normalization (100%) and for categorical encoding. This last one does not create problems if you are using OHE, but some options - such as target encoding - are very sensitive in term of data leakage as well, and you should only take the target of the training set (therefore operate after the split).

The order of the other things is not necessarily fixed. I would always deal with missing data first, as otherwise some numerical operations will give you problems. The last two operations are not necessarily related so should be interchangeable (one is on categoricals, the other on numericals), but since you might encode some categorical variables with continuous numbers I would probably do it first in case you then want to have every numerical variable to be standardized or normalized.

$\endgroup$

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.