/
measures.py
252 lines (189 loc) · 8.6 KB
/
measures.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""ranking_measures.measures: a rank-ordering evaluation package for Python
=========================================================================
ranking_eval is a set of common ranking algorithms such as:
*dcg
*ndcg
*precision
*precision_k
*average_precision
*rankdcg
rankdcg is a new measure and it is described in this paper:
RankDCG is described in this paper:
"RankDCG: Rank-Ordering Evaluation Measure," Denys Katerenchuk, Andrew Rosenberg
https://arxiv.org/abs/1803.00719
"""
__author__ = "Denys Katerenchuk, The Graduate Center, CUNY"
__license__ = """The MIT License (MIT)
Copyright (c) [2015] [Denys Katerenchuk]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE."""
__version__ = "1.0.1"
import math
def find_dcg(element_list):
"""
Discounted Cumulative Gain (DCG)
The definition of DCG can be found in this paper:
Azzah Al-Maskari, Mark Sanderson, and Paul Clough. 2007.
"The relationship between IR effectiveness measures and user satisfaction."
Parameters:
element_list - a list of ranks Ex: [5,4,2,2,1]
Returns:
score
"""
score = 0.0
for order, rank in enumerate(element_list):
score += float(rank)/math.log((order+2))
return score
def find_ndcg(reference, hypothesis):
"""
Normalized Discounted Cumulative Gain (nDCG)
Normalized version of DCG:
nDCG = DCG(hypothesis)/DCG(reference)
Parameters:
reference - a gold standard (perfect) ordering Ex: [5,4,3,2,1]
hypothesis - a proposed ordering Ex: [5,2,2,3,1]
Returns:
ndcg_score - normalized score
"""
return find_dcg(hypothesis)/find_dcg(reference)
def find_ndcg_with_mapping(reference, hypothesis):
"""Normalized Discounted Cumulative Gain (nDCG) with mapping to the original values.
This function takes the prediction of hypothesis and maps the predicted
values to the reference values. This will prevent going onver 1.0 if the predictions
are overestimated.
Parameters:
reference - a gold standard (perfect) ordering Ex: [5,4,3,2,1]
hypothesis - a proposed ordering Ex: [5,2,2,3,1]
Returns:
ndcg_score - normalized score
"""
# with ties, large numbers are put at the end.
reference, hypothesis = _order_lists(reference, hypothesis)
# sorts the predictions
sorted_hypothesis = sorted([x for x in enumerate(hypothesis)], key=lambda x: x[1], reverse=True)
# maps to the real values
mapped_hypothesis = [reference[x[0]] for x in sorted_hypothesis]
# sort true values
ordered_reference = sorted(reference, reverse=True)
return find_ndcg(ordered_reference, mapped_hypothesis)
def find_precision_k(reference, hypothesis, k):
"""
Precision at k
This measure is similar to precision but takes into account first k elements
Description reference:
Kishida, Kazuaki. "Property of average precision and its generalization:
An examination of evaluation indicator for information retrieval experiments."
Tokyo, Japan: National Institute of Informatics, 2005.
Parameters:
reference - a gold standard (perfect) ordering Ex: [5,4,3,2,1]
hypothesis - a proposed ordering Ex: [5,2,2,3,1]
k - a number of top element to consider
Returns:
precision - a score
"""
precision = 0.0
relevant = 0.0
for i, value in enumerate(hypothesis[:k]):
if value == reference[i]:
relevant += 1.0
precision = relevant/k
return precision
def find_precision(reference, hypothesis):
"""
Presision
Description reference:
Kishida, Kazuaki. "Property of average precision and its generalization:
An examination of evaluation indicator for information retrieval experiments."
Tokyo, Japan: National Institute of Informatics, 2005.
Parameters:
reference - a gold standard (perfect) ordering Ex: [5,4,3,2,1]
hypothesis - a proposed ordering Ex: [5,2,2,3,1]
Returns:
precision - a score
"""
return find_precision_k(reference, hypothesis, len(reference))
def find_average_precision(reference, hypothesis):
"""
Average Precision
Description reference:
Kishida, Kazuaki. "Property of average precision and its generalization:
An examination of evaluation indicator for information retrieval experiments."
Tokyo, Japan: National Institute of Informatics, 2005.
Parameters:
reference - a gold standard (perfect) ordering Ex: [5,4,3,2,1]
hypothesis - a proposed ordering Ex: [5,2,2,3,1]
Returns:
precision - a score
"""
s_total = sum([find_precision_k(reference, hypothesis, k+1) for k in \
range(len(reference))])
return s_total/len(reference)
def _order_lists(reference, hypothesis):
"""
Maps and orders both lists. Ex: ref:[2,5,1,1] and hyp:[2,2,3,1] =>
ref:[5,2,1,1] and hyp:[1,2,5,1]
"""
pair_ref_list = sorted([x for x in enumerate(reference)], key=lambda x: x[1])
mapped_hyp_list = [hypothesis[x[0]] for x in pair_ref_list]
return [x[1] for x in pair_ref_list], mapped_hyp_list
def find_rankdcg(reference, hypothesis):
"""
RankDCG - modified version of well known DCG measure.
This measure was designed to work with ties and non-normal rank distribution.
Description reference:
RankDCG is described in this paper:
"RankDCG: Rank-Ordering Evaluation Measure," Denys Katerenchuk, Andrew Rosenberg
https://arxiv.org/abs/1803.00719
Cost function: relative_rank(i)/reversed_rel_rank(i)
Params:
reference_list - list: original list with correct user ranks
hypothesis_list - list: predicted user ranks
Returns:
score - double: evaluation score
"""
# with ties, large numbers are put at the end (needed for consistency)
reference_list, hypothesis_list = _order_lists(reference, hypothesis)
ordered_list = reference_list[:] # creating ordered list
ordered_list.sort(reverse=True)
high_rank = float(len(set(reference_list))) # max rank
reverse_rank = 1.0 # min score (reversed rank)
relative_rank_list = [high_rank]
reverse_rank_list = [reverse_rank]
for index, rank in enumerate(ordered_list[:-1]):
if ordered_list[index+1] != rank:
high_rank -= 1.0
reverse_rank += 1.0
relative_rank_list.append(high_rank)
reverse_rank_list.append(reverse_rank)
# map real rank to relative rank
reference_pair_list = [x for x in enumerate(reference_list)]
sorted_reference_pairs = sorted(reference_pair_list, key=lambda p: p[1], \
reverse=True)
rel_rank_reference_list = [0] * len(reference_list)
for position, rel_rank in enumerate(relative_rank_list):
rel_rank_reference_list[sorted_reference_pairs[position][0]] = rel_rank
# computing max/min values (needed for normalization)
max_score = sum([rank/reverse_rank_list[index] for index, rank \
in enumerate(relative_rank_list)])
min_score = sum([rank/reverse_rank_list[index] for index, rank \
in enumerate(reversed(relative_rank_list))])
# computing and mapping hypothesis to reference
hypothesis_pair_list = [x for x in enumerate(hypothesis_list)]
sorted_hypothesis_pairs = sorted(hypothesis_pair_list, \
key=lambda p: p[1], reverse=True)
eval_score = sum([rel_rank_reference_list[pair[0]] / reverse_rank_list[index] \
for index, pair in enumerate(sorted_hypothesis_pairs)])
return (eval_score - min_score) / (max_score - min_score)