-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_evaluation.py
57 lines (42 loc) · 1.42 KB
/
map_evaluation.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
def get_map(rec, hid):
user_songs = []
# build the dict to index songs
with open("kaggle_songs.txt", "r") as f:
its = dict(map(lambda line: list((line.strip().split(' ')[1],line.strip().split(' ')[0])), f.readlines()))
f.close()
# Read rec songs for each user
rec = "MSD_result.txt"
with open(rec) as f:
for line in f:
song_set = line.strip().split(" ")
for i in range(len(song_set)):
song_set[i] = its[song_set[i]]
user_songs.append(song_set)
# Read hidden songs for each users from file
hid = "Result_hidden.txt"
user_index = -1
user_hidden = []
f = open(hid)
user_list = []
for line in f:
user, song,_, = line.split("\t")
if user not in user_list:
user_list.append(user)
user_hidden.append([])
user_index += 1
user_hidden[user_index].append(song)
# Calculate map values
sum = 0.0
for i in range(0, len(user_hidden)):
value = 0.0
for hidden_song in range(0, len(user_hidden[i])):
for rec_song in range(0, len(user_songs[i])):
if user_hidden[i][hidden_song] == user_songs[i][rec_song]:
value += (hidden_song + 1.0) / (rec_song + 1.0)
value /= len(user_hidden[i])
sum += value
sum /= len(user_hidden)
return sum
rec = ""
hid = ""
print get_map(rec, hid)