How to implement KNN to impute categorical features in a sklearn pipeline

谁说胖子不能爱 提交于 2021-02-08 09:43:09

问题


I want to use KNN for imputing categorical features in a sklearn pipeline (muliple Categorical features missing).

I have done quite a bit research on existing KNN solution (fancyimpute, sklearn KneighborRegressor). None of them seem to be working in terms

  • work in a sklearn pipeline
  • impute categorical features

Some of my questions are (any advice is highly appreciated):

  1. is there any existing approach to allow using KNN (or any other regressor) to impute missing values (categorical in this case) to work with sklearn pipeline
  2. fancyimpute KNN implementation seems not use hamming distance for imputing missing values (which is ideal for categorical features).
  3. is there any fast KNN method implementation available considering KNN is time consuming when imputing missing values (i.e., run prediction on missing values against the whole datasets)

回答1:


  1. The default KNeighborRegressor is supposed to be able to work with regressing missing values, however, with numeric values only. Therefore for categorical value, I believe you most likely need to encode it first, then impute the missing values.

  2. KNNImpute, most likely uses mean/mode etc

  3. iterativeimputer from sklearn can run the imputation against the whole datasets




回答2:


  1. KNNImputer is new as of sklearn version 0.22.0

  2. KNNImputer uses a euclidean distance metric by default, but you can pass in your own custom distance metric.

  3. I can't speak to the speed of KNNImputer, but I'd imagine there have been some optimizations done on it if it's made it into sklearn.



来源:https://stackoverflow.com/questions/57775064/how-to-implement-knn-to-impute-categorical-features-in-a-sklearn-pipeline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!