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

ความแปรปรวนของ feature ใน PCA

ชุดข้อมูลปลามีทั้งหมด 6 มิติ แต่ มิติที่แท้จริง ของมันคือเท่าไร? สร้างกราฟแสดงความแปรปรวนของ feature ใน PCA เพื่อหาคำตอบ เช่นเดิม samples คืออาร์เรย์ 2 มิติ ที่แต่ละแถวแทนปลาหนึ่งตัว คุณต้องทำการ standardize feature ก่อน

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

Unsupervised Learning ใน Python

ดูคอร์ส

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

  • สร้าง instance ของ StandardScaler ชื่อว่า scaler
  • สร้าง instance ของ PCA ชื่อว่า pca
  • ใช้ฟังก์ชัน make_pipeline() เพื่อสร้าง pipeline ที่เชื่อม scaler และ pca เข้าด้วยกัน
  • ใช้เมธอด .fit() ของ pipeline เพื่อ fit กับข้อมูลปลาใน samples
  • ดึงจำนวน component ที่ใช้ผ่าน attribute .n_components_ ของ pca นำค่านี้ใส่ในฟังก์ชัน range() แล้วเก็บผลลัพธ์เป็น features
  • ใช้ฟังก์ชัน plt.bar() เพื่อพล็อตกราฟ explained variance โดยให้ features อยู่บนแกน x และ pca.explained_variance_ อยู่บนแกน y

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

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

# Perform the necessary imports
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt

# Create scaler: scaler
scaler = ____

# Create a PCA instance: pca
pca = ____

# Create pipeline: pipeline
pipeline = ____

# Fit the pipeline to 'samples'
____

# Plot the explained variances
features = ____
plt.bar(____, ____)
plt.xlabel('PCA feature')
plt.ylabel('variance')
plt.xticks(features)
plt.show()
แก้ไขและรันโค้ด