3
$\begingroup$

I was playing around with some data to practice my Python and machine learning skills and wanted to create polynomial features from two features that I think are related and have a strong influence on the predicted output.

Unfortunately my data has missing values (np.NaN) and sklearn's PolynomialFeatures() can not handle these values. What is the best way to impute these values?

I've been trying to replace them with 0, 1, mean and median and for my dataset using the median seems to be the best solution. But can this be generalized and what is the intuition behind it?

I was also wondering if filling methods like ffill, bfill or even KNN modelling can be useful in this context.

Thanks a lot!

$\endgroup$

1 Answer 1

2
$\begingroup$

There is no globally - one could say even locally - ideal way to deal with missing data. This aspect points to incompleteness in the data you're feeding your algorithms, and imputing is simply a technique meant to fill gaps.

Data imputation's motivation is to make feature distribution in your sets the closest possible to whatever real-world distribution it attempts to portray.

The intuition behind why the median is what worked better for your scenario is not something I could be precise about without having access to the data you worked upon, but the positive result is deeply related to your feature distribution, which for the missing points in your data is better represented by the feature's median than all other metrics you've calculated. I'd recommend reading material such as this article that both explains and shows implementation of different imputation techniques - KNN, as you mentioned, being one of them. One of the advantages is seeing how different methods work for a given distribution:

Imputation techniques over time series data

As you have already pointed in the end of your question, methods such as KNN are some straightforward means of imputation that could benefit your case better than mean/median imputation. The biggest difference between those is that KNN better preserves the variance in your data, whereas mean imputation (as you can see from the above image) shifts missing data towards a single value.

Since no method is 100% globally optimal, I'd advise you to try them - KNN, Multiple Interpolation and the likes - and compare. Invest the appropriate amount of time on the techniques that make the most sense for your data.

$\endgroup$
1
  • $\begingroup$ "Data imputation's motivation is to make feature distribution in your sets the closest possible to whatever real-world distribution it attempts to portray." I can understand how it can be done for a numerical feature (compare histogram of before and after imputation) but what about a categorical feature? How do I ensure that the distribution (what kind of plot) remains same or not. $\endgroup$
    – spectre
    Nov 10, 2021 at 4:48

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.