- TP:true positive,将正类预测为正类
- FN:false negative,将正类预测为负类
- FP:false positive,将负类预测为正类
- TN:true negative,将负类预测为负类
伪阳性率FPR(False Positive Rate,在真实为阴性的样本中,被误诊为阴性的比率,在真实为阴性(FP+TN)的样本中,被误诊为阳性(FP)的比值):
FPR=FPFP+TN
F
P
R
=
F
P
F
P
+
T
N
真阳性率TPR(True Positive Rate,在真实为阳性的样本中,被正确诊断为阳性的比率为):
TPR=TPTP+FN
T
P
R
=
T
P
T
P
+
F
N
Precision(精确率):
P=TPTP+FP
P
=
T
P
T
P
+
F
P
Recall(召回率)(FN:false negative,真实为 positive,预测为 negative,伪阴):
R=TPTP+FN
R
=
T
P
T
P
+
F
N
F1 score:
2F1=1P+1RF1=2PRP+R
2
F
1
=
1
P
+
1
R
F
1
=
2
P
R
P
+
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