การลดมิติของข้อมูลการวัดปลา
ในแบบฝึกหัดก่อนหน้า คุณได้เห็นแล้วว่า 2 เป็นตัวเลือกที่เหมาะสมสำหรับ "intrinsic dimension" ของข้อมูลการวัดปลา ตอนนี้ให้ใช้ PCA เพื่อลดมิติของข้อมูลดังกล่าว โดยเก็บไว้เพียง 2 components ที่สำคัญที่สุด
ข้อมูลการวัดปลาได้ถูก scale ไว้ให้แล้ว และพร้อมใช้งานในตัวแปร scaled_samples
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Unsupervised Learning ใน Python
คำแนะนำการฝึกหัด
- นำเข้า
PCAจากsklearn.decomposition - สร้าง PCA instance ชื่อ
pcaโดยกำหนดn_components=2 - ใช้เมธอด
.fit()ของpcaเพื่อ fit กับข้อมูลการวัดปลาที่ผ่านการ scale แล้ว (scaled_samples) - ใช้เมธอด
.transform()ของpcaเพื่อแปลงscaled_samplesแล้วเก็บผลลัพธ์ไว้ในตัวแปรpca_features
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import PCA
____
# Create a PCA model with 2 components: pca
pca = ____
# Fit the PCA instance to the scaled samples
____
# Transform the scaled samples: pca_features
pca_features = ____
# Print the shape of pca_features
print(pca_features.shape)