import os
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
#打开文件,读取预测值和实际值
with open(os.path.join('divide_data','add_phy_cnn_attention.txt'),'r')as f:
lines=f.readlines()
pred1=[float(line.strip().split()[0]) for line in lines]
label1 = [float(line.strip().split()[1]) for line in lines]
#求average precision
cnn_phy_a_ap = average_precision_score(label1, pred1)
#求相应的precision和recall曲线
lr_precision_phy_cnn_a, lr_recall_phy_cnn_a, _ = precision_recall_curve(label1, pred1)
#绘制图线
plt.figure(1)
#传值,绘制图线
#lw参数是线的宽度
#label是线说明
#其他参数[https://www.runoob.com/matplotlib/matplotlib-line.html](https://www.runoob.com/matplotlib/matplotlib-line.html)
plt.plot(lr_precision_phy_cnn_a, lr_recall_phy_cnn_a, lw = 2, label='phy cnn attention(AP:'+str(cnn_phy_a_ap)+')')
#字体大小
fontsize = 14
#设置横轴和纵轴的标识以及字体大小
plt.xlabel('Recall', fontsize = fontsize)
plt.ylabel('Precision', fontsize = fontsize)
#图的标题
plt.title('Precision Recall Curve')
plt.legend()
plt.savefig('./test.png')
# plt.show()
PR图(precision---recall)
最新推荐文章于 2023-10-23 20:08:32 发布