-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
66 lines (55 loc) · 2.32 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
65
66
import numpy as np
ROUND_DECIMALS = 2
def get_metrics(fragment_filename, k_candidates, total_relevants):
coincidences = [1 if filename == fragment_filename else 0 for filename in k_candidates]
def precision_topk():
return coincidences.count(1) / len(k_candidates)
def recall_topk():
if total_relevants == 0:
return 0
return coincidences.count(1) / total_relevants
def f1_score_topk():
precision = precision_topk()
recall = recall_topk()
if precision + recall == 0:
return 0
return 2 * (precision * recall) / (precision + recall)
# def mrr():
# for i, candidate in enumerate(k_candidates):
# if candidate == fragment_filename:
# return 1 / (i + 1)
# return 0
# def map():
# def average_precision(fragment_filename, k_candidates):
# relevant_count = 0
# precision_sum = 0
# for i, candidate in enumerate(k_candidates, 1):
# if candidate == fragment_filename:
# relevant_count += 1
# precision_sum += relevant_count / i
# return precision_sum / (1 if relevant_count == 0 else relevant_count)
# ap_sum = 0
# for i in range(len(k_candidates)):
# ap_sum += average_precision(fragment_filename, k_candidates)
# return ap_sum / len(k_candidates)
# def ndcg():
# def dcg(k_candidates):
# return sum((1 / np.log2(i + 2) if candidate == fragment_filename else 0)
# for i, candidate in enumerate(k_candidates))
# ideal_dcg = 1 / np.log2(1 + 1)
# actual_dcg = dcg(k_candidates)
# return actual_dcg / ideal_dcg
metrics = {
"precision@k": round(precision_topk(), ROUND_DECIMALS),
"recall@k": round(recall_topk(), ROUND_DECIMALS),
"F1-score": round(f1_score_topk(), ROUND_DECIMALS)
}
return metrics
# # input fragment
# fragment_filename = 'fragment.jpg'
# # input k candidates
# k_candidates = ['fragment.jpg' for _ in range(5)]
# # k_candidates = ['fragment.jpg', 'fragment.jpg', 'fragmen5t.jpg', 'fragment7.jpg', 'fragmen7t.jpg']
# # number of all relevant documents in dataset
# total_relevants = 6
# print(get_metrics(fragment_filename, k_candidates, total_relevants))