ก้าวแรกสู่การทำ Bagging
ได้เห็นแล้วว่าเกิดอะไรขึ้นในการวนซ้ำหนึ่งรอบของ bagging ensemble ทีนี้มาสร้างโมเดล bagging แบบกำหนดเองกัน!
มีฟังก์ชันสองตัวเตรียมไว้ให้แล้ว:
def build_decision_tree(X_train, y_train, random_state=None):
# Takes a sample with replacement,
# builds a "weak" decision tree,
# and fits it to the train set
def predict_voting(classifiers, X_test):
# Makes the individual predictions
# and then combines them using "Voting"
ในทางเทคนิค ฟังก์ชัน build_decision_tree() คือสิ่งที่ทำไปในแบบฝึกหัดก่อนหน้า ที่นี่จะสร้าง tree หลายต้นแล้วรวมผลลัพธ์เข้าด้วยกัน ลองดูว่า ensemble ของโมเดล "อ่อนแอ" เหล่านี้จะช่วยเพิ่มประสิทธิภาพได้หรือไม่!
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Ensemble Methods ใน Python
คำแนะนำการฝึกหัด
- สร้างโมเดลแต่ละตัวโดยเรียกฟังก์ชัน
build_decision_tree()พร้อมส่ง training set และ indexiเป็น random state - ทำนาย label ของชุดทดสอบโดยใช้
predict_voting()พร้อมส่ง list ของ classifierclf_listและ feature ของชุดทดสอบ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Build the list of individual models
clf_list = []
for i in range(21):
weak_dt = ____
clf_list.append(weak_dt)
# Predict on the test set
pred = ____
# Print the F1 score
print('F1 score: {:.3f}'.format(f1_score(y_test, pred)))