【问题标题】:add precision-recall curves to plot using a function使用函数添加精确召回曲线以绘制
【发布时间】:2022-01-19 19:44:42
【问题描述】:

我有一个对许多分类器具有精确度和召回率的数据框,每个分类器都有 4 个不同的置信阈值:

    MODEL   CONFIDENCE_THR  PRECISION   RECALL
0   Model1  0.25    0.992647    0.950704
1   Model1  0.45    1.000000    0.929577
2   Model1  0.35    0.992537    0.936620
3   Model1  0.30    0.992593    0.943662
4   Model2  0.45    0.992647    0.950704
5   Model2  0.30    0.992647    0.950704
6   Model2  0.35    0.992647    0.950704
7   Model2  0.25    0.992701    0.957746
8   Model3  0.30    0.978417    0.957746
9   Model3  0.35    0.978102    0.943662
.
.
.

我想在 JupyterLab 中创建一个 Matplotlib 图,并为每个模型添加一条精确召回曲线。由于模型列表将来可能会发生变化,因此我想使用 Python 函数来执行此操作,而不是在 Matplotlib 代码中硬编码模型名称。我写了类似的东西

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib widget

df = pd.read_csv("results.csv")

plt.title('Precision-Recall curves')
plt.legend(loc = 'lower right')

def plot_precision_recall_curve(df, model, plt):
    df = df.loc[df['MODEL'] == model, ['MODEL', 'PRECISION', 'RECALL']]
    plt.plot(df['RECALL'], df["PRECISION"], 'b', label = model)  

plot_precision_recall_curve(df, 'Model1', plt)
plt.show()

但是我得到一个空的情节,带有消息

No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

另外,请注意,在我的代码中,我尝试绘制单个模型的精确召回曲线,但实际上我想将数据框中包含的所有模型的精确召回曲线添加到同一个图中。你能帮帮我吗?

【问题讨论】:

    标签: python matplotlib jupyter-lab precision-recall


    【解决方案1】:

    要修复你的代码,在你绘制完之后移动plt.legend(),最好在plt.show()之前:

    plt.title('Precision-Recall curves')
    plot_precision_recall_curve(df, 'Model1', plt)
    plt.legend(loc = 'lower right')
    plt.show()
    

    另一方面,您是否对seaborn 开放:

    import seaborn as sns
    sns.lineplot(data=df, x='RECALL', y='PRECISION', hue='MODEL')
    

    输出:

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 1970-01-01
      • 2020-04-18
      • 2021-09-13
      • 2017-09-21
      • 2017-02-11
      • 2020-01-01
      • 2021-12-14
      • 2018-02-23
      相关资源
      最近更新 更多