視覺化插補結果
分析插補結果並選擇最佳方法,通常需要大量試驗。重要的是要確保在插補的同時,資料不會產生偏差。 在前兩個練習中,你使用平均值、中位數、眾數,以及常數填補建立了 4 種不同的插補。
在這個練習中,你要為先前插補後的 DataFrame 繪製散佈圖。為了達成這點,你會建立一個字典,將各個 DataFrame 以其標題作為鍵來對應。
diabetes_mean、diabetes_median、diabetes_mode 和 diabetes_constant 這些 DataFrame 已為你載入。
本練習屬於課程
在 Python 中處理遺漏值
練習說明
- 建立 4 個子圖:配置為 2 列、2 欄的繪圖版面。
- 建立字典
imputations,將每個鍵對應到其相符的 DataFrame。 - 以迴圈同時走訪
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()