Principal Component แรก
Principal component แรกของข้อมูลคือทิศทางที่ข้อมูลมีความแปรปรวนมากที่สุด ในแบบฝึกหัดนี้ ให้ใช้ PCA เพื่อหา principal component แรกจากการวัดความยาวและความกว้างของตัวอย่างเมล็ดพืช แล้วแสดงผลเป็นลูกศรบน scatter plot
อาร์เรย์ grains เก็บข้อมูลความยาวและความกว้างของตัวอย่างเมล็ดพืช โดย PyPlot (plt) และ PCA ถูก import ไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Unsupervised Learning ใน Python
คำแนะนำการฝึกหัด
- สร้าง scatter plot ของข้อมูลเมล็ดพืช (ส่วนนี้ทำไว้ให้แล้ว)
- สร้างอินสแตนซ์
PCAและตั้งชื่อว่าmodel - Fit โมเดลกับข้อมูล
grains - ดึงพิกัดค่าเฉลี่ยของข้อมูลโดยใช้ attribute
.mean_ของmodel - ดึง principal component แรกของ
modelโดยใช้ attribute.components_[0,:] - พล็อต principal component แรกเป็นลูกศรบน scatter plot โดยใช้ฟังก์ชัน
plt.arrow()และระบุอาร์กิวเมนต์สองตัวแรกเป็นmean[0]และmean[1]
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Make a scatter plot of the untransformed points
plt.scatter(grains[:,0], grains[:,1])
# Create a PCA instance: model
model = ____
# Fit model to points
____
# Get the mean of the grain samples: mean
mean = ____
# Get the first principal component: first_pc
first_pc = ____
# Plot first_pc as an arrow, starting at mean
plt.arrow(____, ____, first_pc[0], first_pc[1], color='red', width=0.01)
# Keep axes on same scale
plt.axis('equal')
plt.show()