预测结果 | ||
---|---|---|
真实情况 | 正例 | 反例 |
正例 | TP | FN |
反例 | FP | TN |
查准率:Precision(P) = TP / (TP + FP) 预测为正例的实例中真正正例的比率
查全率:Recall(R)=TP / (TP + FN) 真正正例被预测为正例的比率
查准率与查全率是一对矛盾的度量
Fscore:= 2 * P * R / (P+R) = 2 * TP / (样例总数 + TP - TN)
python中有直接计算的工具:sklearn
from sklearn.metrics import confusion_matrix,precision_recall_fscore_support
actual = [1, 1, 1, 1, 0,1,0]
predict = [1, 1, 1, 1, 1,1,1]
ravel = confusion_matrix(actual,predict)
if len(ravel) == 1:
tn,fp,fn,tp = 0,0,ravel.ravel(),0
precision,recall,fscore = [1,1],[1,1],[1,1]
else:
tn,fp,fn,tp = ravel.ravel()
precision,recall,fscore,support_micro = precision_recall_fscore_support(actual,predict)
print(tn,fp,fn,tp)
print(precision[1],recall[1],fscore[1])
需要注意的是,如果actual,predict完全一样,ravel的值是[[样本数]]
precision 会把0,1的情况都计算出来,不过一般只关注1的,因此这里都只取precision[1]
结果: