-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmap.py
44 lines (33 loc) · 845 Bytes
/
map.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
'''
Compute MAP score for given test and prediction files
Usage: map.py test_with_answers.csv predictions.txt
Test file is supposed to have headers in first line
Predictions file is supposed to NOT have headers
'''
import sys, csv
test_file = sys.argv[1]
predictions_file = sys.argv[2]
t = open( test_file )
p = open( predictions_file )
t_reader = csv.reader( t )
headers = t_reader.next()
p_reader = csv.reader( p, delimiter = " " )
n = 0
m = 0
aps = []
for line in t_reader:
n += 1
predictions = p_reader.next()
sku = line[1]
if sku not in predictions:
aps.append( 0 )
continue
m += 1
i = predictions.index( sku )
ap = 1.0 / ( i + 1 )
#print ap
aps.append( ap )
map_score = sum( aps ) / n
print predictions_file
print "Found clicked SKU in %s / %s cases (%s)" % ( m, n, 1.0 * m / n )
print "MAP: %s" % ( map_score )