Python multi classification problem PR curve drawing (including code)

Time:2022-5-9

import numpy as np
import matplotlib.pyplot as plt
score_ Path = “. / pr_curve. TXT” # file path
with open(score_path, ‘r’) as f:

Files = f.readlines() # read files

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)) # record all scores and sort them from small to large to find each threshold point
micro_precis = []
micro_recall = []
for i in lis_order:

true_ P0 = 0 # true Yang
true_ N0 = 0 # true Yin
false_ P0 = 0 # false positive
false_ N0 = 0 # false negative
true_p1 = 0
true_n1 = 0
false_p1 = 0
false_n1 = 0
true_p2 = 0
true_n2 = 0
false_p2 = 0
false_n2 = 0
for file in files:
    cls, pd, n0, n1, n2 = file. strip(). Split ("") # respectively calculate and compare the scores of each category. They are divided into two categories,
                                                        #Finally, find the average and get the macro pr
    If float (N0) > = float (I) and CLS = = '0': # traverse all samples. Class 0 is positive and other classes are negative,
        true_ p0 = true_ P0 + 1 # is greater than or equal to the threshold, and the true positive sample is true Yang,
    elif float(n0) >= float(i) and cls != ' 0 ': # greater than or equal to the threshold, the real sample is negative, that is, false positive;
        false_ p0 = false_ If P0 + 1 # is less than the threshold, the true sample is positive, that is, false negative
    elif float(n0) < float(i) and cls == '0':
        false_n0 = false_n0 + 1
    If float (N1) > = float (I) and CLS = = '1': # traverse all samples. Type 1 is positive and other types are negative
        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': # traverse all samples. Type 2 is positive and other types are negative
        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 + 1
Prec0 = (true_p0 + 0.00000000000 1) / (true_p0 + false_p0 + 0.00000000000 1) # calculate the accuracy rate of each category. The decimal prevents the denominator from being 0
Prec1 = [skrill Download]( 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.00000000000 1) / (true_p0 + false_n0 + 0.00000000000 1) # calculate the recall rate of each category. The decimal and denominator are 0
recall1 = (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)/3
Recall = (recall0 + recall1 + recall2) / 3 # multi classification to obtain the average accuracy and average recall, i.e. macro micro_ pr
micro_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()