mean average precision 即对所有类的average precision的平均,一个类的averege precision计算有两种,
一种是11点插值法,voc 08年以前用。11-points interpolated average precision as defined by TREC. This is the average of the maximum precision for recall levels greather than 0.0, 0.1, 0.2, ..., 1.0. This measure was used in the PASCAL VOC challenge up to the 2008 edition.
一种是Average precision as defined by TREC. This is the average of the precision observed each time a new positive sample is recalled。计算方法是找到recall变化时(TP被recall时)的点,求这些点对应的precision的均值。下面是两种计算AP的计算script
# fp和tp是Nsamples*1的0/1array,比如fp表示测试样本是否在false positive,是则为1。
fp = np.cumsum(fp)
tp = np.cumsum(tp)
rec = tp / float(npos)
prec = tp / (tp + fp)
ap = voc_ap(rec, prec, use_07_metric)
def
voc_ap(rec, prec, use_07_metric=False):
""" ap = voc_ap(rec, prec, [use_07_metric])
Compute VOC AP given precision and recall.
If use_07_metric is true, uses the
VOC 07 11 point method (default:False).
"""
if use_07_metric:
# 11 point metric ap = 0.
for t
in np.arange(0., 1.1, 0.1):
if np.sum(rec >= t) == 0:
p = 0
else:
p = np.max(prec[rec >= t])
ap = ap + p / 11.
else:
# correct AP calculation # first append sentinel values at the end mrec = np.concatenate(([0.], rec, [1.]))
mpre = np.concatenate(([0.], prec, [0.]))
# compute the precision envelope
for i
in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
# to calculate area under PR curve, look for points # where X axis (recall) changes value i = np.where(mrec[1:] != mrec[:-1])[0]
# and sum (\Delta recall) * prec ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap