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

การ Decorrelate ข้อมูลการวัดเมล็ดพืชด้วย PCA

จากแบบฝึกหัดก่อนหน้า จะเห็นว่าการวัดความกว้างและความยาวของเมล็ดพืชมีความสัมพันธ์กัน ในแบบฝึกหัดนี้ จะใช้ PCA เพื่อ decorrelate ข้อมูลเหล่านี้ จากนั้นพล็อตจุดที่ผ่านการ decorrelate แล้ว และวัดค่าสหสัมพันธ์เพียร์สัน (Pearson correlation)

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

Unsupervised Learning ใน Python

ดูคอร์ส

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

  • นำเข้า PCA จาก sklearn.decomposition
  • สร้าง instance ของ PCA โดยตั้งชื่อว่า model
  • เรียกใช้เมธอด .fit_transform() ของ model เพื่อนำ PCA transformation ไปใช้กับ grains แล้วเก็บผลลัพธ์ไว้ในตัวแปร pca_features
  • โค้ดส่วนที่เหลือสำหรับการดึงข้อมูล พล็อตกราฟ และคำนวณค่าสหสัมพันธ์เพียร์สันของสองคอลัมน์แรกใน pca_features ได้เขียนไว้ให้แล้ว กด ส่งคำตอบ เพื่อดูผลลัพธ์ได้เลย!

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Import PCA
____

# Create PCA instance: model
model = ____

# Apply the fit_transform method of model to grains: pca_features
pca_features = ____

# Assign 0th column of pca_features: xs
xs = pca_features[:,0]

# Assign 1st column of pca_features: ys
ys = pca_features[:,1]

# Scatter plot xs vs ys
plt.scatter(xs, ys)
plt.axis('equal')
plt.show()

# Calculate the Pearson correlation of xs and ys
correlation, pvalue = pearsonr(xs, ys)

# Display the correlation
print(correlation)
แก้ไขและรันโค้ด