混淆矩阵
真实值 1 | 真实值0 | |
---|---|---|
预测值 1 | 真正例 TP | 伪正例 FP |
预测值 0 | 伪反例 FN | 真反例TN |
查准率/正确率 :
P = T P T P + F P P=\frac{TP}{TP+FP} P=TP+FPTP
查全率/召回率 :
R = T P T P + F N R=\frac{TP}{TP+FN} R=TP+FNTP
F值:正确率和召回率的调和平均值
F 1 = 2 P R P + R F1 = \frac{2PR}{P+R} F1=P+R2PR
代码实现
def precision_recall(gt, preds):
'''
input: gt(ground_truth), preds(predictions)
output: precision, recall
'''
TP = 0
FP = 0
FN = 0
for t in gt:
if t in preds:
TP += 1
else:
FN += 1
for p in preds:
if p not in gt:
FP += 1
if TP+FP == 0:
precision = 0
else:
precision = TP/float(TP+FP)
if TP+FN == 0:
recall = 0
else:
recall = TP/float(TP+FN)
return precision, recall
参考链接:DeepLearningProject