-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetrics.py
64 lines (41 loc) · 1.24 KB
/
metrics.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
from math import log
def auc(result,total_relavent):
rel_so_far = 0.0
ans = np.zeros(11)
for i in range(len(result)):
if(result[i]==1):
rel_so_far+=1
precision = rel_so_far/(i+1)
recall = rel_so_far/total_relavent
x = int(recall*100 // 10)
while(x>=0 and x<11 and ans[x]==0):
ans[x] = precision
x = x-1
# print ans
return np.mean(ans)
def calc_ndgc(result):
sorted_result = sorted(result,reverse = True)
idgc = sorted_result[0]
for i in range(1,len(sorted_result)):
idgc += sorted_result[i]*1.0/(log(i+1,2))
dgc = result[0]
for i in range(1,len(result)):
dgc += result[i]*1.0/(log(i+1,2))
return dgc/idgc
def calc_precision(result):
rel_so_far = 0.0
for i in range(len(result)):
if(i==5):
p5 = rel_so_far/5
elif (i==10):
p10 = rel_so_far/10
elif (i==20):
p20 = rel_so_far/20
break
if(result[i]==1):
rel_so_far+=1
return np.array([p5,p10,p20])
if __name__ == '__main__':
print auc([0,1,0,0,1,0,0,1],3)
print ndgc([1,1,0,1])