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

การใช้ PCA

ในแบบฝึกหัดนี้ คุณจะนำ PCA ไปใช้กับชุดข้อมูล wine เพื่อดูว่าสามารถเพิ่มความแม่นยำของโมเดลได้หรือไม่

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

การเตรียมข้อมูลสำหรับ Machine Learning ด้วย Python

ดูคอร์ส

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

  • สร้างออบเจกต์ PCA
  • กำหนด features (X) และ labels (y) จาก wine โดยใช้ labels ในคอลัมน์ "Type"
  • นำ PCA ไปใช้กับ X_train และ X_test โดยไม่ให้เกิด data leakage แล้วเก็บค่าที่แปลงแล้วเป็น pca_X_train และ pca_X_test
  • แสดงค่า attribute .explained_variance_ratio_ ของ pca เพื่อตรวจสอบว่าแต่ละ component อธิบายความแปรปรวนได้มากน้อยแค่ไหน

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

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

# Instantiate a PCA object
pca = ____()

# Define the features and labels from the wine dataset
X = wine.drop(____, ____)
y = wine["Type"]

X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)

# Apply PCA to the wine dataset X vector
pca_X_train = ___.____(____)
pca_X_test = ___.____(____)

# Look at the percentage of variance explained by the different components
print(____)
แก้ไขและรันโค้ด