เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Clustered heatmaps

Heatmap มีประโยชน์มากในการแสดง correlation matrix แต่ clustermap นั้นดียิ่งกว่า เพราะช่วยให้มองเห็นโครงสร้างใน correlation matrix ได้ชัดขึ้นด้วยการสร้าง heatmap แบบ hierarchical clustering:

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)

เพื่อป้องกันไม่ให้ label ของแกนทับซ้อนกัน สามารถอ้างอิง Axes จากออบเจ็กต์ fig และกำหนดค่าการหมุนได้ ศึกษาอาร์กิวเมนต์ของฟังก์ชัน clustermap() เพิ่มเติมได้ที่นี่

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การแสดงภาพข้อมูล Time Series ด้วย Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Import seaborn โดยใช้ชื่อย่อว่า sns
  • คำนวณค่าสหสัมพันธ์ระหว่างทุกคอลัมน์ใน DataFrame meat โดยใช้วิธี Pearson และเก็บผลลัพธ์ไว้ในตัวแปรใหม่ชื่อ corr_meat
  • พล็อต clustermap ของ 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()
แก้ไขและรันโค้ด