图像多标签标注准确率(accuracy)、精确率(precision)、召回率(recall)和F1值计算

参考链接:https://blog.csdn.net/weixin_41650458/article/details/82555775

  • 图像多标签标注准确率(accuracy)[mAP]、精确率(precision)、召回率(recall)和F1值计算
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 13 21:09:48 2019

@author: muli
"""
import numpy as np 

# 矩阵每行为一个样本, 即本案例中,有10张图片
# 列数 表示标签种类数,即本案例中,有5类标签 ====》 0表示没有,1表示有

y_true=np.array([[0,0,1,1,0],
   [0,0,0,0,1],
   [0,0,1,1,0],
   [0,0,1,1,0],
   [0,0,1,1,0],
   [0,0,1,1,0],
   [0,0,1,1,0],
   [0,0,0,0,1],
   [0,0,1,0,0],
   [0,0,0,0,1]])


y_pred=np.array([[0,0,1,0,0],
         [0,0,0,0,1],
         [0,0,1,1,0],
         [0,0,0,1,0],
         [0,0,0,0,0],
         [0,0,0,1,0],
         [0,0,0,1,0],
         [0,0,0,0,1],
         [0,0,1,0,0],
         [0,0,0,0,1]])


# True Positive:即y_true与y_pred中同时为1的个数
TP = np.sum(np.logical_and(np.equal(y_true, 1), np.equal(y_pred, 1)))   # 10
#TP = np.sum(np.multiply(y_true, y_pred)) #同样可以实现计算TP
# False Positive:即y_true中为0但是在y_pred中被识别为1的个数
FP = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 1)))   #  0
# False Negative:即y_true中为1但是在y_pred中被识别为0的个数
FN = np.sum(np.logical_and(np.equal(y_true, 1), np.equal(y_pred, 0)))  # 6
# True Negative:即y_true与y_pred中同时为0的个数
TN = np.sum(np.logical_and(np.equal(y_true, 0), np.equal(y_pred, 0)))  # 34

# 根据上面得到的值计算A、P、R、F1
A=(TP+TN)/(TP+FP+FN+TN) #y_pred与y_ture中同时为1或0
P=TP/(TP+FP) #y_pred中为1的元素同时在y_true中也为1
R=TP/(TP+FN) #y_true中为1的元素同时在y_pred中也为1
F1=2*P*R/(P+R) 
    
print(A) # A=44/50
print(P) # P=10/10
print(R) # R=10/16
print(F1) 
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值