Created
November 22, 2017 00:36
-
-
Save h3ct0r/11fb0bb80e4a43d0e39af15b889f136b to your computer and use it in GitHub Desktop.
Calculate recall and precision of a hit/miss query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calc_precision(rel): | |
""" | |
return the precision from a series of hit or miss data | |
""" | |
p = [] | |
hit = 0 | |
for i in xrange(len(rel)): | |
if rel[i] > 0: | |
hit += 1 | |
if hit > 0: | |
p.append(hit / float(i + 1)) | |
else: | |
p.append(0) | |
return p | |
def calc_delta_recall(recall): | |
""" | |
return the recall delta (k - k-1) | |
from a series of recall data | |
""" | |
delta_r = [] | |
for i in xrange(len(recall)): | |
if i <= 0: | |
delta_r.append(recall[i]) | |
else: | |
delta_r.append(recall[i] - recall[i - 1]) | |
return delta_r | |
def calc_recall(rel): | |
""" | |
return the recall from a series of hit or miss data | |
""" | |
r = [] | |
hit = 0 | |
total_hit = float(rel.count(1)) | |
for i in xrange(len(rel)): | |
if rel[i] > 0: | |
hit += 1 | |
r.append(hit / total_hit) | |
return r | |
def ap(rel, use_delta=False): | |
""" | |
Calculate average precision of a query | |
use_delta papram switchs between: | |
using the delta of the recall or using the normal average formula | |
https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Mean_average_precision | |
""" | |
ap = [] | |
p = calc_precision(rel) | |
if use_delta: | |
r = calc_recall(rel) | |
dr = calc_delta_recall(r) | |
for i in xrange(len(rel)): | |
ap.append(p[i] * dr[i]) | |
return sum(ap) | |
else: | |
for i in xrange(len(rel)): | |
if rel[i] > 0: | |
ap.append(p[i]) | |
return sum(ap) / float(len(ap)) | |
# queries mapped from 1 (hit) or 0 (miss) | |
a_rel = [1, 1, 1, 0, 1, 0, 0, 1, 1, 0] | |
b_rel = [0, 1, 1, 1, 1, 1, 1, 0, 0, 0] | |
c_rel = [1, 0, 1, 1, 1, 1, 0, 0, 0, 1] | |
d_rel = [1, 1, 0, 1, 0, 0, 0, 0, 0, 0] | |
e_rel = [1, 0, 0, 1, 0, 0, 0, 1, 0, 0] | |
f_rel = [0, 1, 1] | |
print ap(a_rel, use_delta=True) | |
print ap(b_rel, use_delta=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment