การปรับ Hyperparameter ของ Bagging
แม้ว่าจะสร้าง bagging classifier ด้วยค่าพารามิเตอร์เริ่มต้นได้อย่างง่ายดาย แต่ขอแนะนำให้ปรับแต่งค่าเหล่านี้เพื่อให้ได้ประสิทธิภาพสูงสุด โดยอุดมคติแล้วควรปรับค่าผ่าน K-fold cross-validation
ในแบบฝึกหัดนี้ ลองดูว่าการปรับพารามิเตอร์ของ bagging classifier จะช่วยให้โมเดลทำงานได้ดีขึ้นหรือไม่
ที่นี่เราส่งพารามิเตอร์ solver='liblinear' ให้กับ LogisticRegression ด้วย เพื่อลดเวลาในการคำนวณ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Ensemble Methods ใน Python
คำแนะนำการฝึกหัด
- สร้าง bagging classifier โดยใช้ logistic regression เป็น base estimator กำหนดจำนวน base estimator เป็น
20ฟีเจอร์สูงสุด10ตัว สัดส่วนของตัวอย่างสูงสุด (max_samples) เป็น0.65(65%) และสุ่มตัวอย่าง โดยไม่ ใส่คืน - ใช้
clf_bagเพื่อพยากรณ์ป้ายกำกับของชุดทดสอบX_test
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Build a balanced logistic regression
clf_base = LogisticRegression(class_weight='balanced', solver='liblinear', random_state=42)
# Build and fit a bagging classifier with custom parameters
clf_bag = ____(____, ____, ____, ____, ____, random_state=500)
clf_bag.fit(X_train, y_train)
# Calculate predictions and evaluate the accuracy on the test set
y_pred = ____
print('Accuracy: {:.2f}'.format(accuracy_score(y_test, y_pred)))
# Print the classification report
print(classification_report(y_test, y_pred))