更改roc曲线的绘图-度量图表中的图例项

问题描述

我正在使用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,"TREAT2"])
bcxgb = BinaryClassification(y_test,predictions3,"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()

enter image description here

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

我的最终目标:更改图例项名称,并且不进行3次随机猜测。

解决方法

这是我的解决方案,提供了一个简单的绘图解决方法(我尝试将变量/颜色放在正确的位置):

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,'b',linewidth=3)                                # Plot 1st ROC Curve
plt.plot(fpr2,'g',linewidth=3)                                # Plot 1st ROC Curve
plt.plot(fpr3,'purple',linewidth=3)                           # Plot 1st ROC Curve
plt.plot(np.arange(0,1.01,0.01),np.arange(0,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. 创建一个新的(非隐藏)图并将#2中的数据绘制为多个序列
  5. 将从#3获取的图例条目修改为仅具有1个“随机猜测”标签,并将其设置为新创建图形的图例。

以下是使用plot-metric的示例代码/数据的示例图: Sample_Figure