我有一個資料框,具有許多分類器的精度和召回率,每個分類器都使用 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.
另外,請注意,在我的代碼中,我試圖繪制單個模型的精度召回曲線,但實際上我想將資料幀中包含的所有模型的精度召回曲線添加到同一圖中。你能幫助我嗎?
uj5u.com熱心網友回復:
要修復您的代碼,請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')
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383653.html
標籤:Python matplotlib jupyter 实验室 精确召回
