クラスター化ヒートマップ
ヒートマップは相関行列の可視化にとても便利ですが、クラスタマップはさらに優れています。クラスタマップは、階層的にクラスタリングしたヒートマップを作成し、相関行列の構造を明らかにします。
df_corr = df.corr()
fig = sns.clustermap(df_corr)
plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
軸ラベルの重なりを防ぐには、背後にある fig オブジェクトから Axes を参照し、回転角度を指定します。clustermap() 関数の引数についてはこちらをご覧ください。
この演習はコースの一部です
Pythonで学ぶ時系列データの可視化
演習の手順
seabornをsnsとしてインポートします。meatDataFrame のすべての列間の相関を Pearson 法で計算し、結果をcorr_meatという新しい変数に代入します。corr_meatのクラスタマップを描画します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Import seaborn library
____
# Get correlation matrix of the meat DataFrame
corr_meat = ____(____)
# Customize the heatmap of the corr_meat correlation matrix and rotate the x-axis labels
fig = ____(corr_meat,
row_cluster=True,
col_cluster=True,
figsize=(10, 10))
plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
plt.show()