python计算PR曲线sklearn.metrics.precision_recall_curve
生活随笔
收集整理的這篇文章主要介紹了
python计算PR曲线sklearn.metrics.precision_recall_curve
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PR曲線實(shí)則是以precision(精準(zhǔn)率)和recall(召回率)這兩個(gè)為變量而做出的曲線,其中recall為橫坐標(biāo),precision為縱坐標(biāo)。設(shè)定一系列閾值,計(jì)算每個(gè)閾值對(duì)應(yīng)的recall和precision,即可計(jì)算出PR曲線各個(gè)點(diǎn)。
precision=tp?/?(tp?+?fp)
recall=tp?/?(tp?+?fn)
可以用sklearn.metrics.precision_recall_curve計(jì)算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是正確標(biāo)簽,y_score是概率輸出值,thresholds是閾值,當(dāng)y_score>=thresholds,則預(yù)測(cè)為正樣本,當(dāng)y_score<thresholds,則預(yù)測(cè)為負(fù)樣本。注意,輸出的precision和recall最后一個(gè)值分別為1和0,并且沒(méi)有對(duì)應(yīng)的閾值。該例子中,正樣本實(shí)際數(shù)量為2個(gè),負(fù)樣本實(shí)際數(shù)量為2個(gè)。
- 當(dāng)index=0,thresholds[index]=0.35,此時(shí)預(yù)測(cè)的標(biāo)簽為[0,1,1,1],tp=2,fp=1,fn=0,所以precision=0.67,recall=1
- 當(dāng)index=1,thresholds[index]=0.4,此時(shí)預(yù)測(cè)的標(biāo)簽為[0,1,0,1],tp=1,fp=1,fn=1,所以precision=0.5,recall=0.5
- 當(dāng)index=2,thresholds[index]=0.8,此時(shí)預(yù)測(cè)的標(biāo)簽為[0,0,0,1],tp=1,fp=0,fn=1,所以precision=1,recall=0.5
總結(jié)
以上是生活随笔為你收集整理的python计算PR曲线sklearn.metrics.precision_recall_curve的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Matlab】滤波器常用命令
- 下一篇: python文本字符串比对_python