1
$\begingroup$

I have 2 datasets, testdf and traindf, they both contain missing values in some features. When filling in the missing values using mean/median imputation should I be using the mean/median of the individual datasets or all of the data?

i.e.

test_missing_value = mean of testdf 

or

test_missing_value = mean of testdf + traindf
$\endgroup$
1
$\begingroup$

Ideally, the training and testing set are completely seperate. When that is the case you calculate the mean/median from the training and testing set and use those values to impute missing values in the training and testing set respectively.

However, when you have to split the training set into two parts to create a testing set and a smaller training set, you may use the mean/median of the whole dataset for imputation, since the entire dataset has been provided to you to train the model you are creating.

Some prefer to compute training and testing set mean/median seperately which is fine too, however, if you have few data points then it is best to use the mean/median from the whole datset.

So for your case I would say that the missing value in the test set can be imputed by the mean/median that you computed for the whole dataset.

test_missing_value = mean of (testdf + traindf)
$\endgroup$
1
$\begingroup$

Imputation with mean of column (feature)

I'd argue that there is a third option:

test_missing_value = mean of traindf

The rationale behind this is that the value to impute is part of the model and thus estimated during model training.


Your two possibilities do have the (potential) disadvantage that prediction (as in testing) needs to work also for a single case. For a single case to be predicted, your first approach doesn't work at all, and the 2nd corresponds to using the training mean.

For a few cases to be predicted, your approaches can be computed. But the imputed value will depend on the choice of cases to be predicted. There is nothing to prevent a user from submitting a batch of the same extreme/edge cases for prediction, leading to a totally different imputed value from what would be reasonable for the overall population of your application.
In contrast, during training you have good control on what cases enter the calculation of the imputed values: you can and should make sure the value to be imputed is calculated from a sufficiently large and representatively chosen data set.

Imputation with mean of row (case)

There are some types of data where it is sensible to impute within each case (e.g. mean of neighboring available wavelengths for spectroscopy). In that case, each case is treated separately and you can do that for each of your test cases.

$\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.