python计算PR曲线sklearn.metrics.precision_recall_curve
生活随笔
收集整理的這篇文章主要介紹了
python计算PR曲线sklearn.metrics.precision_recall_curve
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
PR曲線實則是以precision(精準率)和recall(召回率)這兩個為變量而做出的曲線,其中recall為橫坐標,precision為縱坐標。設定一系列閾值,計算每個閾值對應的recall和precision,即可計算出PR曲線各個點。
precision=tp?/?(tp?+?fp)
recall=tp?/?(tp?+?fn)
可以用sklearn.metrics.precision_recall_curve計算PR曲線
from sklearn.metrics import precision_recall_curve y_true = [0, 0, 1, 1] y_score = [0.1, 0.4, 0.35, 0.8]precision, recall, thresholds = precision_recall_curve(y_true, y_score) print(precision) print(recall) print(thresholds) """ [0.66666667 0.5 1. 1.] [1. 0.5 0.5 0. ] [0.35 0.4 0.8 ] """其中y_true是正確標簽,y_score是概率輸出值,thresholds是閾值,當y_score>=thresholds,則預測為正樣本,當y_score<thresholds,則預測為負樣本。注意,輸出的precision和recall最后一個值分別為1和0,并且沒有對應的閾值。該例子中,正樣本實際數(shù)量為2個,負樣本實際數(shù)量為2個。
- 當index=0,thresholds[index]=0.35,此時預測的標簽為[0,1,1,1],tp=2,fp=1,fn=0,所以precision=0.67,recall=1
- 當index=1,thresholds[index]=0.4,此時預測的標簽為[0,1,0,1],tp=1,fp=1,fn=1,所以precision=0.5,recall=0.5
- 當index=2,thresholds[index]=0.8,此時預測的標簽為[0,0,0,1],tp=1,fp=0,fn=1,所以precision=1,recall=0.5
總結
以上是生活随笔為你收集整理的python计算PR曲线sklearn.metrics.precision_recall_curve的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Matlab】滤波器常用命令
- 下一篇: linux高亮查找关键字