更改 roc 曲线的 plot-metrics 图表中的图例项

Change legend items in plot-metrics chart of roc curve

我正在使用 plot-metrics 库来创建 ROC 图表。

我正在尝试为我创建的三个不同模型创建图表,以便在它们之间进行比较并显示哪个模型是最好的。 问题是我无法编辑图例,随机猜测出现了 3 次 + 无法编辑图例中的项目名称(例如模型 1、模型 2 和模型 3)。

这就是我生成此图表的方式:

from plot_metric.functions import BinaryClassification
# Visualisation with plot_metric
bcl = BinaryClassification(y_test, predictions1, labels=["TREAT1", "TREAT2"])
bcrf = BinaryClassification(y_test, predictions2, labels=["TREAT1", "TREAT2"])
bcxgb = BinaryClassification(y_test, predictions3, labels=["TREAT1", "TREAT2"])

# Figures
plt.figure(figsize=(5,5))
bcl.plot_roc_curve(plot_threshold=False,c_roc_curve='b', title='Receiver Operating Characteristic')
bcrf.plot_roc_curve(plot_threshold=False,c_roc_curve='green')
bcxgb.plot_roc_curve(plot_threshold=False,c_roc_curve='purple')

plt.show()

我以为有这个参数(随机猜测是真还是假)但只有阈值和其他参数,也找不到图例的任何参数:
https://plot-metric.readthedocs.io/en/latest/

我的最终目标:更改图例项名称,而不是随机猜测 3 次。

这是我的解决方案,有一个简单的图 work-around(我试着把你的 variables/colors 放在正确的位置):

import numpy as np

# Get data and labels (via hidden figure)
plt.figure(figsize=(1,1))
ax = plt.gca()
ax.set_visible(False)
fpr1, tpr1, _, auc1 = bcl.plot_roc_curve(plot_threshold=False,c_roc_curve='r')
fpr2, tpr2, _, auc2 = bcrf.plot_roc_curve(plot_threshold=False,c_roc_curve='g', ls_random_guess='')
fpr3, tpr3, _, auc3 = bbcxgb.plot_roc_curve(plot_threshold=False,c_roc_curve='m', ls_random_guess='')

# Get series labels as numpy list
_, labels = ax.get_legend_handles_labels()
labels = np.array(labels)                     

# Create plot yourself
fig = plt.figure(figsize=(15,10))                                     # Init figure
plt.plot(fpr1, tpr1, 'b', linewidth=3)                                # Plot 1st ROC Curve
plt.plot(fpr2, tpr2, 'g', linewidth=3)                                # Plot 1st ROC Curve
plt.plot(fpr3, tpr3, 'purple', linewidth=3)                           # Plot 1st ROC Curve
plt.plot(np.arange(0,1.01,0.01), np.arange(0,1.01,0.01), linewidth=3) # Plot dashed guess line
plt.legend([labels[0],labels[2],labels[4],labels[1]])                 # Fix legend entries
plt.xlabel('False Positive Rate [FPR]')
plt.ylabel('True Positive Rate [TPR]')
plt.title('Receiver Operating Characteristic')                        # Add appropriate title

我的代码工作如下:

  1. 创建一个 1x1(或微小的)隐藏图形用于绘图
  2. 运行 plot_roc_curve 并获取 3 个模型中每个模型的 FPR、TPR 和 AUC 值
  3. 使用 plt.gca()ax.get_legend_handles_labels() 获取隐藏地块的图例条目。
  4. 创建一个新的 (non-hidden) 图并将来自 #2 的数据绘制为多个系列
  5. 修改从#3 获取的图例条目,使其只有 1 个 'Random guess' 标签,并将其设置为新创建图形的图例。

这是使用 plot-metric 的示例 code/data: 的示例图