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

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()
แก้ไขและรันโค้ด