您的位置:首页 > 编程语言 > Python开发

分类模型的精确率(precision)与召回率(recall)(Python)

2016-01-11 17:21 1431 查看
TP:true positive,将正类预测为正类

FN:false negative,将正类预测为负类

FP:false positive,将负类预测为正类

TN:true negative,将负类预测为负类



伪阳性率FPR(False Positive Rate,在真实为阴性的样本中,被误诊为阴性的比率):

FPR=FPFP+TN

真阳性率TPR(True Positive Rate,在真实为阳性的样本中,被正确诊断为阳性的比率为):

TPR=TPTP+FN

Precision(精确率)

P=TPTP+FP

Recall(召回率)

R=TPTP+FN

F1 score:

2F1=1P+1RF1=2PRP+R

# y_true, y_pred
# TP = (y_pred==1)*(y_true==1)
# FP = (y_pred==1)*(y_true==0)
# FN = (y_pred==0)*(y_true==1)
# TN = (y_pred==0)*(y_true==0)
# TP + FP = y_pred==1
# TP + FN = y_true==1

def precision_score(y_true, y_pred):
return ((y_true==1)*(y_pred==1)).sum()/(y_pred==1).sum()
def recall_score(y_true, y_pred):
return ((y_true==1)*(y_pred==1)).sum()/(y_true==1).sum()
def f1_score(y_true, y_pred):
num = 2*precison_score(y_true, y_pred)*recall_score(y_true, y_pred)
deno = (precision_score(y_true, y_pred)+recall_score(y_true, y_pred))
return num/deno
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: