Recursive Feature Elimination แบบอัตโนมัติ
ทีนี้มาทำให้กระบวนการ recursive นี้เป็นแบบอัตโนมัติกัน ครอบ Recursive Feature Eliminator (RFE) รอบ estimator ถดถอยโลจิสติกของเรา แล้วกำหนดจำนวนฟีเจอร์ที่ต้องการ
ฟังก์ชันและแพ็กเกจที่จำเป็นทั้งหมดได้โหลดไว้ล่วงหน้าแล้ว และฟีเจอร์ได้รับการปรับสเกลให้เรียบร้อย
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การลดมิติข้อมูลใน Python
คำแนะนำการฝึกหัด
- สร้าง RFE โดยใช้ estimator
LogisticRegression()และกำหนดให้เลือก 3 ฟีเจอร์ - พิมพ์ฟีเจอร์และอันดับของแต่ละฟีเจอร์
- พิมพ์ฟีเจอร์ที่ไม่ถูกตัดออก
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create the RFE with a LogisticRegression estimator and 3 features to select
rfe = ____(estimator=____, n_features_to_select=____, verbose=1)
# Fits the eliminator to the data
rfe.fit(X_train, y_train)
# Print the features and their ranking (high = dropped early on)
print(dict(zip(X.columns, rfe.____)))
# Print the features that are not eliminated
print(X.columns[rfe.____])
# Calculates the test set accuracy
acc = accuracy_score(y_test, rfe.predict(X_test))
print(f"{acc:.1%} accuracy on test set.")