-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate.py
77 lines (55 loc) · 2.55 KB
/
evaluate.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
# -*- coding: utf-8 -*-
import fire
import numpy as np
from arena_util import load_json
class ArenaEvaluator:
def _idcg(self, l):
return sum((1.0 / np.log(i + 2) for i in range(l)))
def __init__(self):
self._idcgs = [self._idcg(i) for i in range(101)]
def _ndcg(self, gt, rec):
dcg = 0.0
for i, r in enumerate(rec):
if r in gt:
dcg += 1.0 / np.log(i + 2)
return dcg / self._idcgs[len(gt)]
def _eval(self, gt_fname, rec_fname):
gt_playlists = load_json(gt_fname)
gt_dict = {g["id"]: g for g in gt_playlists}
rec_playlists = load_json(rec_fname)
gt_ids = set([g["id"] for g in gt_playlists])
rec_ids = set([r["id"] for r in rec_playlists])
if gt_ids != rec_ids:
raise Exception("결과의 플레이리스트 수가 올바르지 않습니다.")
rec_song_counts = [len(p["songs"]) for p in rec_playlists]
rec_tag_counts = [len(p["tags"]) for p in rec_playlists]
if set(rec_song_counts) != set([100]):
raise Exception("추천 곡 결과의 개수가 맞지 않습니다.")
if set(rec_tag_counts) != set([10]):
raise Exception("추천 태그 결과의 개수가 맞지 않습니다.")
rec_unique_song_counts = [len(set(p["songs"])) for p in rec_playlists]
rec_unique_tag_counts = [len(set(p["tags"])) for p in rec_playlists]
if set(rec_unique_song_counts) != set([100]):
raise Exception("한 플레이리스트에 중복된 곡 추천은 허용되지 않습니다.")
if set(rec_unique_tag_counts) != set([10]):
raise Exception("한 플레이리스트에 중복된 태그 추천은 허용되지 않습니다.")
music_ndcg = 0.0
tag_ndcg = 0.0
for rec in rec_playlists:
gt = gt_dict[rec["id"]]
music_ndcg += self._ndcg(gt["songs"], rec["songs"][:100])
tag_ndcg += self._ndcg(gt["tags"], rec["tags"][:10])
music_ndcg = music_ndcg / len(rec_playlists)
tag_ndcg = tag_ndcg / len(rec_playlists)
score = music_ndcg * 0.85 + tag_ndcg * 0.15
return music_ndcg, tag_ndcg, score
def evaluate(self, gt_fname, rec_fname):
try:
music_ndcg, tag_ndcg, score = self._eval(gt_fname, rec_fname)
print(f"Music nDCG: {music_ndcg:.6}")
print(f"Tag nDCG: {tag_ndcg:.6}")
print(f"Score: {score:.6}")
except Exception as e:
print(e)
if __name__ == "__main__":
fire.Fire(ArenaEvaluator)