import numpy as np
import matplotlib.pyplot as plt
score_path = "./pr_curve.txt" # 文件门路
with open(score_path, 'r') as f:
files = f.readlines() # 读取文件
lis_all = []
for file in files:
_, _, s1, s2, s3 = file.strip().split(" ")lis_all.append(s1)lis_all.append(s2)lis_all.append(s3)
lis_order = sorted(set(lis_all)) # 记录所有得分状况,并去重从小到大排序,寻找各个阈值点
micro_precis = []
micro_recall = []
for i in lis_order:
true_p0 = 0 # 真阳true_n0 = 0 # 真阴false_p0 = 0 # 假阳false_n0 = 0 # 假阴true_p1 = 0true_n1 = 0false_p1 = 0false_n1 = 0true_p2 = 0true_n2 = 0false_p2 = 0false_n2 = 0for file in files: cls, pd, n0, n1, n2 = file.strip().split(" ") # 别离计算比拟各个类别的得分,离开计算,各自为二分类, # 最初求均匀,得出宏pr if float(n0) >= float(i) and cls == '0': # 遍历所有样本,第0类为正样本,其余类为负样本, true_p0 = true_p0 + 1 # 大于等于阈值,并且实在为正样本,即为真阳, elif float(n0) >= float(i) and cls != '0': # 大于等于阈值,实在为负样本,即为假阳; false_p0 = false_p0 + 1 # 小于阈值,实在为正样本,即为假阴 elif float(n0) < float(i) and cls == '0': false_n0 = false_n0 + 1 if float(n1) >= float(i) and cls == '1': # 遍历所有样本,第1类为正样本,其余类为负样本 true_p1 = true_p1 + 1 elif float(n1) >= float(i) and cls != '1': false_p1 = false_p1 + 1 elif float(n1) < float(i) and cls == '1': false_n1 = false_n1 + 1 if float(n2) >= float(i) and cls == '2': # 遍历所有样本,第2类为正样本,其余类为负样本 true_p2 = true_p2 + 1 elif float(n2) >= float(i) and cls != '2': false_p2 = false_p2 + 1 elif float(n2) < float(i) and cls == '2': false_n2 = false_n2 + 1prec0 = (true_p0+0.00000000001) / (true_p0 + false_p0 + 0.000000000001) # 计算各类别的准确率,小数避免分母为0prec1 =[Skrill下载](https://www.gendan5.com/wallet/Skrill.html) (true_p1+0.00000000001) / (true_p1 + false_p1 + 0.000000000001)prec2 = (true_p2+0.00000000001) / (true_p2 + false_p2 + 0.000000000001)recall0 = (true_p0+0.00000000001)/(true_p0+false_n0 + 0.000000000001) # 计算各类别的召回率,小数避免分母为0recall1 = (true_p1+0.00000000001) / (true_p1 + false_n1+0.000000000001)recall2 = (true_p2+0.00000000001)/(true_p2+false_n2 + 0.00000000001)precision = (prec0 + prec1 + prec2)/3recall = (recall0 + recall1 + recall2)/3 # 多分类求得均匀精确度和均匀召回率,即宏micro_prmicro_precis.append(precision)micro_recall.append(recall)
micro_precis.append(1)
micro_recall.append(0)
print(micro_precis)
print(micro_recall)
x = np.array(micro_recall)
y = np.array(micro_precis)
plt.figure()
plt.xlim([-0.01, 1.01])
plt.ylim([-0.01, 1.01])
plt.xlabel('recall')
plt.ylabel('precision')
plt.title('PR curve')
plt.plot(x, y)
plt.show()