通过从现有值的分布中抽样来估算缺失值

Impute missing values by sampling from the distribution of existing ones

缺失值是数据分析中的常见问题。一种常见的策略似乎是将缺失值替换为从现有值的分布中随机抽取的值。

是否有 Python 库代码可以方便地对数据框执行此预处理步骤?据我所知,sklearn.preprocessing 模块不提供此策略。

要从现有值的分布中抽样,您需要知道分布。如果分布未知,您可以使用核密度估计来拟合它。这篇博客 post 很好地概述了 Python 的核密度估计实现:http://jakevdp.github.io/blog/2013/12/01/kernel-density-estimation/

scikit-learn 中有一个实现(参见 http://scikit-learn.org/stable/modules/density.html#kernel-density); sklearn's KernelDensity has .sample() method. There is also a kernel density estimator in statsmodels (http://statsmodels.sourceforge.net/devel/generated/statsmodels.nonparametric.kernel_density.KDEMultivariate.html);它支持分类特征。

另一种方法是选择随机的现有值,而不尝试生成数据集中未见的值。此解决方案的问题是值可能取决于同一行中的其他值,random.sample 不考虑这一点可能会产生不切实际的示例。