如何在python中处理机器学习中缺少的NAN

如何在python中处理机器学习中缺少的NAN,python,pandas,machine-learning,missing-data,Python,Pandas,Machine Learning,Missing Data,在应用机器学习算法之前,如何处理数据集中的缺失值??。 我注意到放弃丢失的NAN值不是一件明智的事情。我通常使用pandas插值(计算平均值)并填充数据,这是一种工作,可以提高分类精度,但可能不是最好的方法 这是一个非常重要的问题处理数据集中缺失值的最佳方法是什么? 例如,如果您看到此数据集,只有30%具有原始数据 Int64Index: 7049 entries, 0 to 7048 Data columns (total 31 columns): left_eye_center_x

在应用机器学习算法之前,如何处理数据集中的缺失值??。

我注意到放弃丢失的NAN值不是一件明智的事情。我通常使用pandas插值(计算平均值)并填充数据,这是一种工作,可以提高分类精度,但可能不是最好的方法

这是一个非常重要的问题处理数据集中缺失值的最佳方法是什么?

例如,如果您看到此数据集,只有30%具有原始数据

Int64Index: 7049 entries, 0 to 7048
Data columns (total 31 columns):
left_eye_center_x            7039 non-null float64
left_eye_center_y            7039 non-null float64
right_eye_center_x           7036 non-null float64
right_eye_center_y           7036 non-null float64
left_eye_inner_corner_x      2271 non-null float64
left_eye_inner_corner_y      2271 non-null float64
left_eye_outer_corner_x      2267 non-null float64
left_eye_outer_corner_y      2267 non-null float64
right_eye_inner_corner_x     2268 non-null float64
right_eye_inner_corner_y     2268 non-null float64
right_eye_outer_corner_x     2268 non-null float64
right_eye_outer_corner_y     2268 non-null float64
left_eyebrow_inner_end_x     2270 non-null float64
left_eyebrow_inner_end_y     2270 non-null float64
left_eyebrow_outer_end_x     2225 non-null float64
left_eyebrow_outer_end_y     2225 non-null float64
right_eyebrow_inner_end_x    2270 non-null float64
right_eyebrow_inner_end_y    2270 non-null float64
right_eyebrow_outer_end_x    2236 non-null float64
right_eyebrow_outer_end_y    2236 non-null float64
nose_tip_x                   7049 non-null float64
nose_tip_y                   7049 non-null float64
mouth_left_corner_x          2269 non-null float64
mouth_left_corner_y          2269 non-null float64
mouth_right_corner_x         2270 non-null float64
mouth_right_corner_y         2270 non-null float64
mouth_center_top_lip_x       2275 non-null float64
mouth_center_top_lip_y       2275 non-null float64
mouth_center_bottom_lip_x    7016 non-null float64
mouth_center_bottom_lip_y    7016 non-null float64
Image                        7049 non-null object
没有最好的方法,每个解决方案/算法都有各自的优缺点(你甚至可以将其中的一些混合在一起,创建自己的策略,并调整相关参数,从而得出一个最能满足你的数据的解决方案/算法,这方面有很多研究/论文)

例如,均值插补快速简单,但它会低估方差,用均值替换NaN会扭曲分布形状,而KNN插补在时间复杂度方面在大型数据集中可能并不理想,因为它迭代所有数据点并对每个NaN值执行计算,并且假设NaN属性与其他属性相关

How to handle missing values in datasets before applying machine learning algorithm??
除了您提到的均值插补外,您还可以查看K-最近邻插补和回归插补,并参考中的强大类来检查要使用的现有API

KNN插补

计算该NaN点的k个最近邻的平均值

回归插补

估计回归模型,以根据其他变量预测变量的观测值,然后在缺少该变量的情况下,使用该模型插补值

链接到scikit的“缺失值插补”部分。
我也听说过插补库,但还没有机会使用它

处理缺失数据没有单一的最佳方法。最严格的方法是在概率框架(如PyMC)中将缺失值建模为附加参数。这样,您将得到可能值的分布,而不仅仅是一个答案。以下是使用PyMC处理缺失数据的示例:


如果你真的想用点估计值来填补这些漏洞,那么你需要执行“插补”。我会避开简单的插补方法,比如均值填充法,因为它们真的会破坏你特征的联合分布。相反,尝试类似的方法(尝试通过低秩近似推断缺少的值)。softImpute的原始版本是为R编写的,但我在这里制作了Python版本(以及kNN插补等其他方法):

“处理数据集中缺失值的最佳方法是什么?”我认为这个问题的答案是针对具体情况的,和基于意见的。您可以删除缺失值的行,但这可能会降低性能,或者将缺失值设置为不影响预测但仍可能扭曲模型的某个值。如果缺失值很多,这取决于具体情况。你可以使用平均值/中位数,但你必须衡量所有方法的性能,看看什么是最好的,这取决于这些特征中是否有任何价值以及你选择的模型
How to handle missing values in datasets before applying machine learning algorithm??