インピュテーションを可視化する
インピュテーションを分析して最適な方法を選ぶには、たくさんの試行が必要です。インピュテーションの過程でデータにバイアスが入らないようにすることが重要です。 直前の2つの演習では、平均値、中央値、最頻値、定数で埋めるという4種類のインピュテーションを作成しました。
この演習では、先ほどインピュテーションした各DataFrameの散布図を作成します。そのために、タイトルをキーとしてDataFrameを値に持つ辞書を作成します。
diabetes_mean、diabetes_median、diabetes_mode、diabetes_constant の各DataFrameは読み込まれています。
この演習はコースの一部です
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()