클러스터형 히트맵
히트맵은 상관행렬을 시각화하는 데 매우 유용하지만, 클러스터맵이 더 좋을 때가 많아요. 클러스터맵은 계층적 클러스터링이 적용된 히트맵을 만들어 상관행렬의 구조를 파악할 수 있게 해줍니다:
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()