什么是PR曲线:用于评估分类模型性能的一种常见评估工具。

召回率:表示分类器识别所有正例样本的能力,就是原来的实际为正例有多少预测对了。TP/TP+FN

精确率:表示分类器预测为正例的样本中实际为正例的比例,就是预测结果中有多少正例。TP/TP+FP

PR曲线是一组(R,P)值组成,横轴是召回率,纵轴是精确率。

代码1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from numpy.core.numeric import normalize_axis_tuple
import pylab
from scipy.interp_spline
readDir = "绝对路径"
Curve_one = [];
Curve_two = [];
Curve_three = [];
Curve_four = [];
Curve_five = [];
drawMat = [];
PRscore = [];
gtMat = [];
dataMat = [];
scoreMat = [];

det savescore(readDir):
f = open(readDir,"r");
Line = f.readLine();
while Line:
txt = Line.split(',');
dataMat.append(int(txt[0]))
score = FLOAT(txt[2]);
scoreMat.append(score);
line = f.readLine();
f.close();
def generategt(geMat)
for i in range(0,len(dataMat),1):
if i>=2311 and i<=4513 :
geMat.append(1);
else:
geMat.append(0)
def calculatetruefalse(i,drawMat,gtMat):
A = 0;
B = 0;
C = 0;
for num in range(0, len(dataMat), 1):
if float(scoreMat[num]) <= i:
drawMat.append(i);
else:
drawMat.append(0);
if (gtMat[num] == 1) and (drawMa[num] == 1):
A = A + 1;
if (gtMat[num] == 0) and (drawMa[num] == 1):
B = B + 1;
if (gtMat[num] == 1) and (drawMa[num] == 0):
C = C + 1;
if (A+B != 0):
precious = A/(A+B);
else:
precious = 0;
if A + C != 0:
recall = A/(A+C)
else:
recall = 0;
PRscore.append([precious,recall])

def draw(Curve_one):
plt.figure();
plot1 = plt.plot(Curve_one[1],Curve_one[0],'r.-',linewidth = 2.5, markersize = 1.0)


def save_Curve(readDir1,drawMat,gtMat,PRscore)
savescore();
generategt();
for i in range(20, 1000, 10):
calculatetruefalse(i/1000, drawMat, gtMat)
drawMat = [];



if __name__ == '__main__';
Curve_one = save_curve(readDir1,drawMat,gtMat,PRscore);
reset();