再次练习混淆矩阵
在 Python 中创建混淆矩阵并不难。最大的挑战在于弄清矩阵的方向。本练习将帮助您理解 sklearn 的混淆矩阵实现。这里,您使用 tic_tac_toe 数据集训练了一个随机森林模型 rfc,用于预测玩家一的对局结果:0(失败)或 1(胜利)。
注意: 如果您在其他网站或其他编程语言中阅读关于混淆矩阵的资料,行列的含义可能是相反的。
本练习是课程的一部分
Python 中的模型验证
练习说明
- 导入
sklearn中用于创建混淆矩阵的函数。 - 使用模型
rfc,在测试集X_test上生成类别预测。 - 使用
sklearn创建混淆矩阵。 - 打印
cm中表示真实为 1 且被预测为 1(真正例)的那个值。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from sklearn.metrics import ____
# Create predictions
test_predictions = rfc.____(____)
# Create and print the confusion matrix
cm = ____(____, ____)
print(cm)
# Print the true positives (actual 1s that were predicted 1s)
print("The number of true positives is: {}".format(cm[____, ____]))