-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.h
More file actions
126 lines (118 loc) · 4.32 KB
/
metrics.h
File metadata and controls
126 lines (118 loc) · 4.32 KB
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
#include <vector>
#include <unordered_set>
#include <cmath>
namespace metrics {
double APK(const std::vector <size_t> &predictions, const std::vector <size_t> &target, size_t k) {
std::unordered_set <size_t> used;
std::unordered_set <size_t> target_set;
for (auto elem: target) {
target_set.insert(elem);
}
double hints = 0, total = 0, score = 0;
for (size_t i = 0; i < std::min(predictions.size(), k); ++i) {
if (target_set.count(predictions[i]) && !used.count(predictions[i])) {
hints += 1;
score += hints / (i + 1.0);
}
}
return score / (0.0 + k);
}
double MAPK(const std::vector <std::vector<size_t>> &predictions,
const std::vector <std::vector<size_t>> &target,
size_t K) {
double sum_apk = 0;
double cnt_all = 0;
for (size_t i = 0; i < target.size(); ++i) {
if (target[i].empty()) {
continue;
}
cnt_all += 1.0;
sum_apk += APK(predictions[i], target[i], K);
}
return sum_apk / cnt_all;
}
double MR(const std::vector <size_t> &predictions, const std::vector <size_t> &target) {
std::unordered_set <size_t> used;
std::unordered_set <size_t> target_set;
for (auto elem: target) {
target_set.insert(elem);
}
double total = 0, score = 0;
for (size_t i = 0; i < predictions.size(); ++i) {
if (target_set.count(predictions[i]) && !used.count(predictions[i])) {
score += 1.0 / (i + 1.0);
break;
}
}
return score;
}
double MRR(const std::vector <std::vector<size_t>> &predictions,
const std::vector <std::vector<size_t>> &target) {
double sum_mrr = 0;
double cnt_all = 0;
for (size_t i = 0; i < target.size(); ++i) {
if (target[i].empty()) {
continue;
}
cnt_all += 1.0;
sum_mrr += MR(predictions[i], target[i]);
}
return sum_mrr / cnt_all;
}
double NDCGUnique(const std::vector <size_t> &predictions,
const std::vector <size_t> &target) {
std::unordered_set <size_t> used;
std::unordered_set <size_t> target_set;
for (auto elem: target) {
target_set.insert(elem);
}
double total = 0, score = 0;
for (size_t i = 0; i < predictions.size(); ++i) {
if (target_set.count(predictions[i]) && !used.count(predictions[i])) {
score += 1.0 / log2(i + 2.0);
}
}
return score;
}
double NDCG(const std::vector <std::vector<size_t>> &predictions,
const std::vector <std::vector<size_t>> &target) {
double sum_mrr = 0;
double cnt_all = 0;
for (size_t i = 0; i < target.size(); ++i) {
if (target[i].empty()) {
continue;
}
cnt_all += 1.0;
sum_mrr += NDCGUnique(predictions[i], target[i]);
}
return sum_mrr / cnt_all;
}
double RecallKUnique(const std::vector <size_t> &predictions, const std::vector <size_t> &target, size_t k) {
std::unordered_set <size_t> used;
std::unordered_set <size_t> target_set;
for (auto elem: target) {
target_set.insert(elem);
}
double total = 0, score = 0;
for (size_t i = 0; i < std::min(predictions.size(), k); ++i) {
if (target_set.count(predictions[i]) && !used.count(predictions[i])) {
score += 1.0 / (k + 0.0);
}
}
return score;
}
double PrecisionK(const std::vector <std::vector<size_t>> &predictions,
const std::vector <std::vector<size_t>> &target,
size_t K) {
double sum_apk = 0;
double cnt_all = 0;
for (size_t i = 0; i < target.size(); ++i) {
if (target[i].empty()) {
continue;
}
cnt_all += 1.0;
sum_apk += RecallKUnique(predictions[i], target[i], K);
}
return sum_apk / cnt_all;
}
}