mAP 的具体实现 python代码

mAP 事每一个类的AP的平均值,因此 算出每一个类的AP 就行。以下代码 是算一个类的AP ,简单的可以认为 预测为车的概率。 关于召回率与准确率 请google。

 

 

import numpy as np

import matplotlib.pyplot as plt


# 第一列是样本id, 第二列是分类的得分, 第三列是ground truth, 1表示车 0 表示不是
# 以下是假设模型已经对20个样本进行了预测,而且我们知道这20个测试样本的真实标签
table = np.array([
[1, 0.23, 0],
[2, 0.76, 1],
[3, 0.01, 0],
[4, 0.91, 1],
[5, 0.13, 0],
[6, 0.45, 0],
[7, 0.12, 1],
[8, 0.03, 0],
[9, 0.38, 1],
[10, 0.11, 0],
[11, 0.03, 0],
[12, 0.09, 0],
[13, 0.65, 0],
[14, 0.07, 0],
[15, 0.12, 0],
[16, 0.24, 1],
[17, 0.1, 0],
[18, 0.23, 0],
[19, 0.46, 0],
[20, 0.08, 1]
])


# 按分数从大到小排序
index = np.argsort(table[:,1])[::-1]

# T是排序后的table
T = table[index]

# 样本总数
length = T.shape[0]

# 正样本总数:1表示正样本,0表示负样本
TP = len(T[T[:,-1] > 0])


result = []

# 计算top-N的 召回率 与 准确率
for i in range(length):
    temp = np.sum(T[: i+1, -1] > 0)
    # 结果取top-N 的某一个值时,temp / TP 表示召回率,temp / (i + 1) 表示准确率
    result.append([temp / TP, temp / (i + 1)])

result1 = np.array(result)

result2 = []

# result 中共有 x 个召回率,x=TP
# 计算每一个召回率对应的最大的 准确率
for i in range(TP):
    result2.append([(i + 1) / TP, np.max(result1[result1[:, 0] == (i + 1) / TP][:, 1])])

result2 = np.array(result2)

result3 = []

"""
N个样本中有M个正例,那么我们会得到M个recall值(1/M, 2/M, ..., M/M),对于每个recall值r,我们可以计算出对应(r' > r)的最大precision,然后对这M个precision值取平均即得到最后的AP值
"""
for i in range(result2.shape[0]):
    result3.append([result2[i, 0], np.max(result2[i:result2.shape[0], 1])])

result3 = np.array(result3)

plt.plot(result3[:, 0], result3[:, 1])
plt.show()

AP = np.mean(result3[:, 1])
print(AP)









 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值