首页 > 代码库 > PR曲线 的 计算及绘制

PR曲线 的 计算及绘制

    在linear model中,我们对各个特征线性组合,得到linear score,然后确定一个threshold,linear score < threshold 判为负类,linear score > threshold 判为正类。画PR曲线时, 我们可以想象threshold 是不断变化的。首先,threshold 特别大,这样木有一个是正类,我们计算出查全率与查准率; 然后 threshold 减小, 只有一个正类,我们计算出查全率与查准率;然后 threshold再减小,有2个正类,我们计算出查全率与查准率;threshold减小一次,多出一个正类,直到所有的类别都被判为正类。 然后以查全率为横坐标,差准率为纵坐标,画出图形即可。

例如,有

实际类别 linear score threshold  为5 threshold  为4 threshold  为3 threshold  为2 threshold  为1  
+ 5.2   + + + + +  
+ 4.45   - + + + +  
- 3.5   - - + + +  
- 2.45 - - - + +  
- 1.65 - - - - +  
    1 / 1 2 / 2 2 / 3   2 / 4 2 / 5 查全率
    1 / 5 2 / 5  3 / 5 4 / 5 5 / 5 差准率

查全率: 预测为正的里面,实际为正的比例。

查准率:预测为正,实际为正 占的比例。

 1 import matplotlib
 2 import numpy as np  
 3 import matplotlib.pyplot as plt  
 4 Recall = np.array([0,1/5,2/5,3/5,4/5,5/5])  #从0开始更加平滑,美观,实际中,数据量很大时,趋近0。
 5 Precison = np.array([1/1,2/2,2/3,2/4,2/5,0])
 6 plt.figure()
 7 plt.ylim(0,1.1)
 8 plt.xlabel("Recall")
 9 plt.xlim(0,1.1)
10 plt.ylabel("Precison")
11 plt.plot(Recall,Precison)
12 plt.show()

 

技术分享

 

PR曲线 的 计算及绘制