Skip to content

Instantly share code, notes, and snippets.

@alexandru-dinu
Created August 24, 2023 12:50
Show Gist options
  • Save alexandru-dinu/236e7a367948fc812c114533821d0296 to your computer and use it in GitHub Desktop.
Save alexandru-dinu/236e7a367948fc812c114533821d0296 to your computer and use it in GitHub Desktop.
Average Precision
def ap_k(res: list[bool], K: int):
out = np.zeros(K, dtype=np.float32)
rel = np.zeros(K, dtype=np.float32)
for k in range(K):
rel[k] = res[k] # is current result relevant?
out[k] = sum(rel) / (k + 1) # how many relevant results until now?
if rel.sum() == 0:
return 0
return (out * rel).sum() / rel.sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment