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

  • TP:true positive,将正类预测为正类
  • FN:false negative,将正类预测为负类
  • FP:false positive,将负类预测为正类
  • TN:true negative,将负类预测为负类


分类模型的精确率(precision)与召回率(recall)(Python)_第1张图片

伪阳性率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

你可能感兴趣的:(分类模型的精确率(precision)与召回率(recall)(Python))