python - 绘制阈值(precision_recall 曲线)matplotlib/sklearn.metrics
问题描述
我正在尝试绘制我的精度/召回曲线的阈值。我只是在使用 MNSIT 数据,其中的示例来自使用 scikit-learn、keras 和 TensorFlow 进行机器学习一书。试图训练模型来检测 5 的图像。我不知道你需要看多少代码。我已经为训练集制作了混淆矩阵,并计算了精度和召回值以及阈值。我已经绘制了 pre/rec 曲线,书中的示例说要添加轴标签、壁架、网格并突出显示阈值,但代码在书中我在下面放置星号的地方被切断了。除了如何让阈值出现在情节上之外,我能够弄清楚所有事情。我已经附上了一张书中图表与我所拥有的图表的图片。这就是本书所展示的:
我无法显示带有两个阈值点的红色虚线。有谁知道我会怎么做?下面是我的代码:
from sklearn.metrics import precision_recall_curve
precisions, recalls, thresholds = precision_recall_curve(y_train_5, y_scores)
def plot_precision_recall_vs_thresholds(precisions, recalls, thresholds):
plt.plot(thresholds, precisions[:-1], "b--", label="Precision")
plt.plot(thresholds, recalls[:-1], "g--", label="Recall")
plt.xlabel("Threshold")
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
plt.grid(b=True, which="both", axis="both", color='gray', linestyle='-', linewidth=1)
plot_precision_recall_vs_thresholds(precisions, recalls, thresholds)
plt.show()
我知道这里有很多关于 sklearn 的问题,但似乎没有一个涵盖让红线出现的问题。我将非常感谢您的帮助!
解决方案
您可以使用以下代码绘制水平线和垂直线:
plt.axhline(y_value, c='r', ls=':')
plt.axvline(x_value, c='r', ls=':')
推荐阅读
- python-3.x - 'For' 循环用于将多个 csv 文件从谷歌存储桶读取到 1 个 Pandas DataFrame
- python - Ajax 表单提交和错误以 JSON 格式出现在模板中(django)
- c++ - 如何通过其持有的数据从链表中删除某个节点?
- asp.net-mvc - 如何在 asp.net-core MVC 中使用 SODA.net 和 App Token
- python - 如何转换列表用户在numpy中输入ed名称?
- python - Google Cloud Function:“错误:超出内存限制。函数调用被中断。” 部署 NLP 模型时
- git - 重命名真正深度的远程 Git 提交消息
- azure - Azure Terraform:azurerm_lb_rule 应该是类型列表,得到类型字符串
- bash - 计算机告诉我文件不存在时
- terraform - VPC内NAT网关公网IP列表