Rac*_*Cyr 5 python matplotlib scikit-learn precision-recall
我正在尝试绘制我的精度/召回曲线的阈值。我只是使用 MNSIT 数据,以及《使用 scikit-learn、keras 和 TensorFlow 进行机器学习实践》一书中的示例。尝试训练模型来检测 5 的图像。我不知道你需要看多少代码。我已经为训练集制作了混淆矩阵,并计算了精度和召回值以及阈值。我已经绘制了预/记录曲线,书中的示例表示添加轴标签、壁架、网格并突出显示阈值,但代码在书中被截断,我在下面放置了星号。我能够弄清楚除了如何让阈值显示在绘图上之外的所有问题。我附上了一张图片,展示了书中的图表与我所拥有的图表的对比。这就是这本书所展示的:
我无法显示带有两个阈值点的红色点线。有谁知道我会怎么做?下面是我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
我知道这里有很多关于 sklearn 的问题,但似乎没有一个问题涉及如何显示红线。我将非常感谢您的帮助!
您可以使用以下代码来绘制水平线和垂直线:
plt.axhline(y_value, c='r', ls=':')
plt.axvline(x_value, c='r', ls=':')
Run Code Online (Sandbox Code Playgroud)