Calculating Average Precision (AP) without Sklearn
import numpy as np
def calculate_ap(recalls, precisions):
# Make precision monotonically non-increasing (all-point interpolation)
m_rec = np.concatenate(([0.0], recalls, [1.0]))
m_pre = np.concatenate(([0.0], precisions, [0.0]))
for i in range(len(m_pre) - 1, 0, -1):
m_pre[i - 1] = np.maximum(m_pre[i - 1], m_pre[i])
# Area under the interpolated step curve: sum of recall steps × precision
indices = np.where(m_rec[1:] != m_rec[:-1])[0]
ap = np.sum((m_rec[indices + 1] - m_rec[indices]) * m_pre[indices + 1])
return ap
Why it matters: Object detection and retrieval metrics break when you only eyeball curves. Manual AP keeps leaderboard numbers reproducible.