【问题标题】:Change legend items in plot-metrics chart of roc curve更改 roc 曲线 plot-metrics 图表中的图例项
【发布时间】:2021-01-09 13:09:53
【问题描述】:

我正在使用 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 次。

【问题讨论】:

    标签: python machine-learning legend roc auc


    【解决方案1】:

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

    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. 创建一个新的(非隐藏)图并将 #2 中的数据绘制为多个系列
    5. 修改从 #3 获取的图例条目,使其仅具有 1 个“随机猜测”标签,并将其设置为新创建图形的图例。

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

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2022-11-01
      • 2013-01-18
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2018-01-03
      • 2019-12-26
      相关资源
      最近更新 更多