การ 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)