使用 KNN 在 python 中进行缺失值插补
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45321406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing value imputation in python using KNN
提问by Clock Slave
I have a dataset that looks like this
我有一个看起来像这样的数据集
1908 January 5.0 -1.4
1908 February 7.3 1.9
1908 March 6.2 0.3
1908 April NaN 2.1
1908 May NaN 7.7
1908 June 17.7 8.7
1908 July NaN 11.0
1908 August 17.5 9.7
1908 September 16.3 8.4
1908 October 14.6 8.0
1908 November 9.6 3.4
1908 December 5.8 NaN
1909 January 5.0 0.1
1909 February 5.5 -0.3
1909 March 5.6 -0.3
1909 April 12.2 3.3
1909 May 14.7 4.8
1909 June 15.0 7.5
1909 July 17.3 10.8
1909 August 18.8 10.7
I want to replace the NaN
s using KNN as the method. I looked up sklearn
s Imputer
class but it supports only mean, median and mode imputation. There is a feature request herebut I don't think thats been implemented as of now. Any ideas on how to replace the NaN
s from the last two columns using KNN?
我想NaN
用 KNN 作为方法来替换s。我查找了sklearn
sImputer
类,但它仅支持均值、中值和模式插补。这里有一个功能请求,但我认为到目前为止还没有实现。关于如何NaN
使用 KNN替换最后两列中的s 的任何想法?
Edit: Since I need to run codes on another environment, I don't have the luxury of installing packages. sklearn, pandas, numpy and other standard packages are the only ones I can use.
编辑:由于我需要在另一个环境中运行代码,我没有安装软件包的奢侈。sklearn、pandas、numpy 和其他标准包是我唯一可以使用的包。
回答by Miriam Farber
fancyimpute packagesupports such kind of imputation, using the following API:
Fantasyimpute 包支持这种类型的插补,使用以下 API:
from fancyimpute import KNN
# X is the complete data matrix
# X_incomplete has the same values as X except a subset have been replace with NaN
# Use 3 nearest rows which have a feature to fill in each row's missing features
X_filled_knn = KNN(k=3).complete(X_incomplete)
Here are the imputations supported by this package:
以下是此包支持的估算:
?SimpleFill: Replaces missing entries with the mean or median of each column.
?KNN: Nearest neighbor imputations which weights samples using the mean squared difference on features for which two rows both have observed data.
?SoftImpute: Matrix completion by iterative soft thresholding of SVD decompositions. Inspired by the softImpute package for R, which is based on Spectral Regularization Algorithms for Learning Large Incomplete Matrices by Mazumder et. al.
?IterativeSVD: Matrix completion by iterative low-rank SVD decomposition. Should be similar to SVDimpute from Missing value estimation methods for DNA microarrays by Troyanskaya et. al.
?MICE: Reimplementation of Multiple Imputation by Chained Equations.
?MatrixFactorization: Direct factorization of the incomplete matrix into low-rank U and V, with an L1 sparsity penalty on the elements of U and an L2 penalty on the elements of V. Solved by gradient descent.
?NuclearNormMinimization: Simple implementation of Exact Matrix Completion via Convex Optimization by Emmanuel Candes and Benjamin Recht using cvxpy. Too slow for large matrices.
?BiScaler: Iterative estimation of row/column means and standard deviations to get doubly normalized matrix. Not guaranteed to converge but works well in practice. Taken from Matrix Completion and Low-Rank SVD via Fast Alternating Least Squares.
?SimpleFill:用每列的平均值或中位数替换缺失的条目。
?KNN:最近邻插补,它使用两行都有观测数据的特征的均方差对样本进行加权。
?SoftImpute:通过SVD 分解的迭代软阈值化来完成矩阵。受 R 的 softImpute 包的启发,该包基于 Mazumder 等人的用于学习大型不完全矩阵的谱正则化算法。阿尔。
? IterativeSVD:通过迭代低秩 SVD 分解来完成矩阵。应该类似于来自 Troyanskaya 等人的 DNA 微阵列缺失值估计方法的 SVDimpute。阿尔。
?MICE:通过链式方程重新实现多重插补。
?MatrixFactorization:将不完整矩阵直接分解为低秩的 U 和 V,对 U 的元素进行 L1 稀疏惩罚,对 V 的元素进行 L2 惩罚。通过梯度下降解决。
?NuclearNormMinimization:Emmanuel Candes 和 Benjamin Recht 使用 cvxpy 通过凸优化简单实现精确矩阵完成。对于大型矩阵来说太慢了。
?BiScaler:迭代估计行/列均值和标准差以获得双重归一化矩阵。不能保证收敛,但在实践中效果很好。通过快速交替最小二乘法取自矩阵完成和低秩 SVD。
回答by Rajshekar Reddy
fancyimpute's KNN imputation no more supports the complete
function as suggested by other answer, we need to now use fit_transform
Fantasyimpute 的 KNN 插补不再支持complete
其他答案所建议的功能,我们现在需要使用fit_transform
# X is the complete data matrix
# X_incomplete has the same values as X except a subset have been replace with NaN
# Use 3 nearest rows which have a feature to fill in each row's missing features
X_filled_knn = KNN(k=3).fit_transform(X_incomplete)
reference https://github.com/iskandr/fancyimpute
回答by Omri
This pull request to sklearn adds KNN support. You can get the code from it - https://github.com/scikit-learn/scikit-learn/pull/9212
这个对 sklearn 的拉取请求增加了 KNN 支持。您可以从中获取代码 - https://github.com/scikit-learn/scikit-learn/pull/9212
回答by amrrs
scikit-learn
v0.22 supports native KNN Imputation
scikit-learn
v0.22 支持原生 KNN 插补
import numpy as np
from sklearn.impute import KNNImputer
X = [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]]
imputer = KNNImputer(n_neighbors=2)
print(imputer.fit_transform(X))
Reference: https://scikit-learn.org/stable/modules/impute.html#knnimpute
参考:https: //scikit-learn.org/stable/modules/impute.html#knnimpute