-
Notifications
You must be signed in to change notification settings - Fork 5
/
ap.py
executable file
·50 lines (46 loc) · 1.21 KB
/
ap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python
'''this script is used to get AP, MAP with file containing records of ranked ground turth to queries and k as input. See paper "Gradient descent optimization of smoothed information retrieval metrics" for more details'''
import sys
import math
import copy
import re
import p
def get_ap(s):
'''This is a function to get average precision'''
a = 0.0
b = 0.0
for i in range(len(s)):
if s[i]>0:
a += p.get_p(s, i+1)
b += 1
#print "Line: %s, ap is %f with a = %f, b = %f"%(s, a/b, a, b)
if b == 0:
b = 1
return a/b
def get_map(s, k):
'''This is a function to get mean average precision'''
a = 0.0
for i in range(len(s)):
if s[i]>0:
a += 1
#print "Line: %sp@%d is %f with a = %f"%(line, k, a/k, a)
return a / k
def test():
if len(sys.argv)!=2:
print "arguments missing"
sys.exit()
f = open(sys.argv[1])
while 1:
line = f.readline()
if not line:
break;
arrays = re.split(r" ",line)
#print arrays
s = [float(i) for i in arrays]
#print s
get_ap(s)
##
## here is the main function
##
if __name__ == '__main__':
test()