在应用机器学习算法之前,如何处理数据集中的缺失值?
我注意到,直接删除缺失的NaN值并不是一个明智的做法。我通常使用pandas进行插值(计算平均值)来填补数据,这种方法在一定程度上有效,并能提高分类准确性,但可能不是最佳方法。
这里有一个非常重要的问题。处理数据集中的缺失值的最佳方法是什么?
例如,如果你看到这个数据集,只有30%的数据是原始数据。
Int64Index: 7049 entries, 0 to 7048Data columns (total 31 columns):left_eye_center_x 7039 non-null float64left_eye_center_y 7039 non-null float64right_eye_center_x 7036 non-null float64right_eye_center_y 7036 non-null float64left_eye_inner_corner_x 2271 non-null float64left_eye_inner_corner_y 2271 non-null float64left_eye_outer_corner_x 2267 non-null float64left_eye_outer_corner_y 2267 non-null float64right_eye_inner_corner_x 2268 non-null float64right_eye_inner_corner_y 2268 non-null float64right_eye_outer_corner_x 2268 non-null float64right_eye_outer_corner_y 2268 non-null float64left_eyebrow_inner_end_x 2270 non-null float64left_eyebrow_inner_end_y 2270 non-null float64left_eyebrow_outer_end_x 2225 non-null float64left_eyebrow_outer_end_y 2225 non-null float64right_eyebrow_inner_end_x 2270 non-null float64right_eyebrow_inner_end_y 2270 non-null float64right_eyebrow_outer_end_x 2236 non-null float64right_eyebrow_outer_end_y 2236 non-null float64nose_tip_x 7049 non-null float64nose_tip_y 7049 non-null float64mouth_left_corner_x 2269 non-null float64mouth_left_corner_y 2269 non-null float64mouth_right_corner_x 2270 non-null float64mouth_right_corner_y 2270 non-null float64mouth_center_top_lip_x 2275 non-null float64mouth_center_top_lip_y 2275 non-null float64mouth_center_bottom_lip_x 7016 non-null float64mouth_center_bottom_lip_y 7016 non-null float64Image 7049 non-null object
回答:
处理数据集中的缺失值的最佳方法是什么?
没有所谓的最佳方法,每种解决方案/算法都有其优缺点(你甚至可以将它们混合使用,创建自己的策略,并调整相关参数以找到最适合你的数据的方法,关于这个话题有很多研究/论文)。
例如,均值填补快速简单,但它会低估方差,并且通过用均值替换NaN会扭曲分布形状,而KNN填补在处理大型数据集时可能在时间复杂度上不是理想的选择,因为它需要遍历所有数据点并为每个NaN值进行计算,并且假设NaN属性与其他属性相关。
在应用机器学习算法之前,如何处理数据集中的缺失值?
除了你提到的均值填补,你还可以考虑K-最近邻填补和回归填补,并参考Imputer类在scikit-learn中的现有API来使用。
KNN填补
计算这个NaN点的k个最近邻的平均值。
回归填补
估计一个回归模型来预测一个变量的观测值基于其他变量,然后使用该模型来填补该变量缺失的情况。