-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
77 lines (66 loc) · 2.46 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import math
import logging
def computeTopNAccuracy(GroundTruth, predictedIndices, topN):
Precision = []
Recall = []
F1 = []
NDCG = []
One_Call = []
MRR = []
# Out the recommendation
# for i in range(len(predictedIndices)):
# if len(GroundTruth[i]) != 0:
# rec_list = []
# for j in range(100):
# rec_list.append(predictedIndices[i][j])
# logging.info('%d: %s'%(i, rec_list))
for index in range(len(topN)):
sumForPrecision = 0
sumForRecall = 0
sumForF1 = 0
sumForNDCG = 0
sumForOne_Call = 0
sumForMRR = 0
for i in range(len(predictedIndices)): # for a user,
if len(GroundTruth[i]) != 0:
mrrFlag = True
userHit = 0
userMRR = 0
dcg = 0
idcg = 0
idcgCount = len(GroundTruth[i])
ndcg = 0
hit = []
for j in range(topN[index]):
if predictedIndices[i][j] in GroundTruth[i]:
# if Hit!
dcg += 1.0 / math.log2(j + 2)
if mrrFlag:
userMRR = (1.0 / (j + 1.0))
mrrFlag = False
userHit += 1
if idcgCount > 0:
idcg += 1.0 / math.log2(j + 2)
idcgCount = idcgCount - 1
if (idcg != 0):
ndcg += (dcg / idcg)
precision_u = userHit / topN[index]
recall_u = userHit / len(GroundTruth[i])
sumForPrecision += precision_u
sumForRecall += recall_u
if (precision_u + recall_u) != 0:
sumForF1 += 2 * (precision_u * recall_u / (precision_u + recall_u))
sumForNDCG += ndcg
sumForOne_Call += 1 if userHit > 0 else 0
sumForMRR += userMRR
Precision.append(sumForPrecision)
Recall.append(sumForRecall)
F1.append(sumForF1)
NDCG.append(sumForNDCG)
One_Call.append(sumForOne_Call)
MRR.append(sumForMRR)
# Precision.append(sumForPrecision / test_userNum)
# Recall.append(sumForRecall / test_userNum)
# NDCG.append(sumForNDCG / test_userNum)
# MRR.append(sumForMRR / test_userNum)
return Precision, Recall, F1, NDCG, One_Call, MRR