This question already has an answer here:

I have a dataset and I'd like to to use it for classification purposes. There are some columns with NULL values that I need to impute. I want to impute with either median or mean but what I want to know if I should impute that with median/mean before spliting into train and test or I should first split into train and test, then impute with median/mean in train data set and take the value for median/mean from the training data set and apply that to my test data set?


marked as duplicate by Sean Easter, Michael Chernick, John, Peter Flom Aug 13 '17 at 10:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Is any of the variables you want to impute the dependent variable, or are they all independent variables? $\endgroup$ – Kodiologist Aug 11 '17 at 17:21
  • $\begingroup$ they are the independant (feature) variable $\endgroup$ – H.Z. Aug 11 '17 at 18:28

In a classification context, it's fine to impute values of the independent variables for all cases before the train–test split (so long as your imputation scheme ignores the dependent variable, as mean or median imputation would). The train–test split is only supposed to hide values of the dependent variable, not the independent variables.


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