开始使用免费开始使用

向冠军发起挑战

您已经将随机森林部署到生产环境,但突然担心朴素贝叶斯分类器可能更好。您想通过冠军-挑战者测试来比较:让朴素贝叶斯作为挑战者,对比当前在生产中的那个完全相同的模型;为避免混淆,您将从文件中加载该模型。您将使用 F1 分数进行评估。您已像之前一样拥有数据 X_trainX_testy_trainy_test,以及 GaussianNB()f1_score()pickle()

本练习是课程的一部分

用 Python 设计机器学习工作流

查看课程

练习说明

  • 使用 pickle 从存储中加载现有模型。
  • 在训练数据上拟合一个高斯朴素贝叶斯分类器。
  • 在测试数据上先打印冠军,再打印挑战者的 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)
编辑并运行代码