-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure.py
58 lines (51 loc) · 2.11 KB
/
measure.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
class Measure:
def __init__(self):
self.hit1 = {"raw": 0.0, "fil": 0.0}
self.hit3 = {"raw": 0.0, "fil": 0.0}
self.hit10 = {"raw": 0.0, "fil": 0.0}
self.mrr = {"raw": 0.0, "fil": 0.0}
self.mr = {"raw": 0.0, "fil": 0.0}
def __add__(self, other):
"""
Add two measure objects
"""
new_measure = Measure()
settings = ["raw", "fil"]
for rf in settings:
new_measure.hit1[rf] = (self.hit1[rf] + other.hit1[rf])
new_measure.hit3[rf] = (self.hit3[rf] + other.hit3[rf])
new_measure.hit10[rf] = (self.hit10[rf] + other.hit10[rf])
new_measure.mrr[rf] = (self.mrr[rf] + other.mrr[rf])
new_measure.mr[rf] = (self.mr[rf] + other.mr[rf])
return new_measure
def __str__(self):
str_list = []
for raw_or_fil in ["raw", "fil"]:
str_list.append("{} setting:".format(raw_or_fil.title()))
str_list.append("\tHit@1 = {}".format(self.hit1[raw_or_fil]))
str_list.append("\tHit@3 = {}".format(self.hit3[raw_or_fil]))
str_list.append("\tHit@10 = {}".format(self.hit10[raw_or_fil]))
str_list.append("\tMR = {}".format(self.mr[raw_or_fil]))
str_list.append("\tMRR = {}".format(self.mrr[raw_or_fil]))
str_list.append("")
return '\n'.join(str_list)
def __repr__(self):
return self.__str__()
def update(self, rank, raw_or_fil):
if rank == 1:
self.hit1[raw_or_fil] += 1.0
if rank <= 3:
self.hit3[raw_or_fil] += 1.0
if rank <= 10:
self.hit10[raw_or_fil] += 1.0
self.mr[raw_or_fil] += rank
self.mrr[raw_or_fil] += (1.0 / rank)
def normalize(self, normalizer):
if normalizer == 0:
return
for raw_or_fil in ["raw", "fil"]:
self.hit1[raw_or_fil] /= (normalizer)
self.hit3[raw_or_fil] /= (normalizer)
self.hit10[raw_or_fil] /= (normalizer)
self.mr[raw_or_fil] /= (normalizer)
self.mrr[raw_or_fil] /= (normalizer)