挑戰冠軍
你已經把 random forest 佈署到正式環境,但忽然擔心 naive Bayes 分類器可能更好。你想執行一個「冠軍—挑戰者」測試:以 naive Bayes 作為挑戰者,和目前正式環境中執行的模型做比較。為了避免混淆,你會從檔案載入該正式環境模型。你將使用 F1 分數來評估。你已經和先前一樣取得 X_train、X_test、y_train 與 y_test,並可使用 GaussianNB()、f1_score() 與 pickle()。
本練習屬於課程
在 Python 設計機器學習工作流程
練習說明
- 使用
pickle從儲存體載入現有模型。 - 對訓練資料訓練一個 Gaussian Naive Bayes 分類器。
- 分別在測試資料上列印冠軍與挑戰者的 F1 分數(先冠軍,後挑戰者)。
- 將效能最佳的模型覆寫到磁碟中的現有模型。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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)