ท้าทายแชมป์
หลังจากนำ random forest ขึ้น production แล้ว คุณเริ่มสงสัยว่า naive Bayes classifier อาจให้ผลลัพธ์ที่ดีกว่า จึงต้องการทดสอบแบบ champion-challenger โดยเปรียบเทียบ naive Bayes ในฐานะ challenger กับโมเดลที่ใช้งานอยู่ใน production ซึ่งจะโหลดจากไฟล์เพื่อให้แน่ใจว่าไม่มีความสับสน โดยใช้คะแนน F1 ในการประเมิน ข้อมูล X_train, X_test, y_train และ y_test พร้อมใช้งานเช่นเดิม รวมถึง GaussianNB(), f1_score() และ pickle()
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การออกแบบ Machine Learning Workflows ด้วย Python
คำแนะนำการฝึกหัด
- โหลดโมเดลที่มีอยู่จากหน่วยความจำโดยใช้
pickle - ฝึก Gaussian Naive Bayes classifier บนชุดข้อมูลฝึก
- แสดงคะแนน F1 ของ champion จากนั้นแสดงคะแนนของ challenger บนชุดข้อมูลทดสอบ
- เขียนทับโมเดลปัจจุบันบนดิสก์ด้วยโมเดลที่ให้ผลลัพธ์ดีที่สุด
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Load the current model from disk
champion = pickle.____(open('model.pkl', ____))
# Fit a Gaussian Naive Bayes to the training data
challenger = ____.____(X_train, y_train)
# Print the F1 test scores of both champion and challenger
print(____(y_test, champion.____(X_test)))
print(____)
# Write back to disk the best-performing model
with open('model.pkl', 'wb') as file:
pickle.____(____, file=file)