可视化插补结果
分析不同的插补方案并选择最佳方案,需要大量试验。插补时务必要确保数据不会产生偏差。 在前两个练习中,您使用均值、中位数、众数和常数填充,共创建了 4 种不同的插补结果。
本练习中,您将为之前插补得到的各个 DataFrame 绘制散点图。为此,您会创建一个字典,将各个 DataFrame 与对应的标题作为键进行映射。
已为您加载 DataFrame:diabetes_mean、diabetes_median、diabetes_mode 和 diabetes_constant。
本练习是课程的一部分
在 Python 中处理缺失数据
练习说明
- 创建 2 行 2 列的图,共 4 个子图。
- 通过将每个键与对应的 DataFrame 配对,创建字典
imputations。 - 同时遍历
axes和imputations,并绘制imputations中的每个 DataFrame。 - 将颜色设置为
nullity,并将每个子图的标题设为对应插补方法的名称。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Set nrows and ncols to 2
fig, axes = plt.subplots(nrows=___, ncols=___, figsize=(10, 10))
nullity = diabetes.Serum_Insulin.isnull()+diabetes.Glucose.isnull()
# Create a dictionary of imputations
imputations = {'Mean Imputation': ___, 'Median Imputation': ___,
'Most Frequent Imputation': ___, 'Constant Imputation': ___}
# Loop over flattened axes and imputations
for ax, df_key in zip(___.___(), ___):
# Select and also set the title for a DataFrame
imputations[___].plot(x='Serum_Insulin', y='Glucose', kind='scatter',
alpha=0.5, c=___, cmap='rainbow', ax=ax,
colorbar=False, title=___)
plt.show()