1. 程式人生 > >PR曲線 的 計算及繪制

PR曲線 的 計算及繪制

一個 2個 lib 平滑 數據 線性 得到 near figure

    在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曲線 的 計算及繪制