แสดงภาพ Correlation Matrix
สามารถนำ correlation matrix ที่สร้างในแบบฝึกหัดก่อนหน้ามาแสดงผลในรูปแบบ heatmap ได้ โดยใช้ฟังก์ชัน heatmap() จากไลบรารี seaborn ซึ่งมีอาร์กิวเมนต์หลายตัวให้ปรับแต่งรูปลักษณ์ของ heatmap ตามต้องการ
df_corr = df.corr()
sns.heatmap(df_corr)
plt.xticks(rotation=90)
plt.yticks(rotation=0)
ใช้เมธอด .xticks() และ .yticks() เพื่อหมุนป้ายกำกับแกน ป้องกันไม่ให้ข้อความซ้อนทับกัน
ดูรายละเอียดอาร์กิวเมนต์ของฟังก์ชัน heatmap() เพิ่มเติมได้ที่หน้านี้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแสดงภาพข้อมูล Time Series ด้วย Python
คำแนะนำการฝึกหัด
- Import
seabornassns - คำนวณค่าสหสัมพันธ์ระหว่างทุกคอลัมน์ใน DataFrame
meatโดยใช้วิธี Spearman แล้วกำหนดผลลัพธ์ให้กับตัวแปรใหม่ชื่อcorr_meat - พล็อต heatmap ของ
corr_meat
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import seaborn library
import ____ as ____
# Get correlation matrix of the meat DataFrame: corr_meat
____ = ____.____(method=____)
# Customize the heatmap of the corr_meat correlation matrix
____(corr_meat,
annot=True,
linewidths=0.4,
annot_kws={"size": 10})
plt.xticks(rotation=90)
plt.yticks(rotation=0)
plt.show()