การ Regularization
Regularization คือกระบวนการเพิ่มข้อมูลเข้าไปในโมเดลเพื่อป้องกันการ overfitting ซึ่งมีความสำคัญอย่างมากในการปรับปรุงเมตริกการประเมินผลที่เรียนไปก่อนหน้านี้ในบทนี้ ในแบบฝึกหัดนี้ จะได้ลองปรับพารามิเตอร์ max depth ของ decision tree เพื่อดูว่าผลการจำแนกประเภทเปลี่ยนแปลงไปอย่างไร
X_train, y_train, X_test, y_test พร้อมใช้งานใน workspace แล้ว รวมถึง pandas ในชื่อ pd, numpy ในชื่อ np, และ sklearn ด้วย นอกจากนี้ยังมี confusion_matrix(), precision_score(), และ recall_score() จาก sklearn.metrics พร้อมให้ใช้งาน
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การพยากรณ์ CTR ด้วย Machine Learning ใน Python
คำแนะนำการฝึกหัด
- สร้าง decision tree หลายแบบโดยปรับค่า maximum depth ของแต่ละต้นไม้ให้แตกต่างกัน
- สำหรับแต่ละต้นไม้ ให้ fit โมเดลและสร้างค่าพยากรณ์บนชุดข้อมูลทดสอบ
- ประเมิน confusion matrix, precision, และ recall ของแต่ละต้นไม้
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Iterate over different levels of max depth
for max_depth_val in [2, 3, 5, 10, 15, 20]:
# Create and fit model
clf = ____(____ = max_depth_val)
print("Evaluating tree with max_depth = %s" %(max_depth_val))
y_pred = clf.fit(____, ____).predict(____)
# Evaluate confusion matrix, precision, recall
print("Confusion matrix: ")
print(____(y_test, y_pred))
prec = ____(____, ____, average = 'weighted')
recall = ____(____, ____, average = 'weighted')
print("Precision: %s, Recall: %s" %(prec, recall))