การใช้ 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(____)