-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate_utils.py
71 lines (62 loc) · 2.75 KB
/
evaluate_utils.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
import numpy as np
import bottleneck as bn
import torch
import math
def computeTopNAccuracy(GroundTruth, predictedIndices, topN):
precision = []
recall = []
NDCG = []
MRR = []
for index in range(len(topN)):
sumForPrecision = 0
sumForRecall = 0
sumForNdcg = 0
sumForMRR = 0
for i in range(len(predictedIndices)):
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)
sumForPrecision += userHit / topN[index]
sumForRecall += userHit / len(GroundTruth[i])
sumForNdcg += ndcg
sumForMRR += userMRR
precision.append(round(sumForPrecision / len(predictedIndices), 4))
recall.append(round(sumForRecall / len(predictedIndices), 4))
NDCG.append(round(sumForNdcg / len(predictedIndices), 4))
MRR.append(round(sumForMRR / len(predictedIndices), 4))
return precision, recall, NDCG, MRR
def print_results(loss, valid_result, test_result):
"""output the evaluation results."""
if loss is not None:
print("[Train]: loss: {:.4f}".format(loss))
if valid_result is not None:
print("[Valid]: Precision: {} Recall: {} NDCG: {} MRR: {}".format(
'-'.join([str(x) for x in valid_result[0]]),
'-'.join([str(x) for x in valid_result[1]]),
'-'.join([str(x) for x in valid_result[2]]),
'-'.join([str(x) for x in valid_result[3]])))
if test_result is not None:
print("[Test]: Precision: {} Recall: {} NDCG: {} MRR: {}".format(
'-'.join([str(x) for x in test_result[0]]),
'-'.join([str(x) for x in test_result[1]]),
'-'.join([str(x) for x in test_result[2]]),
'-'.join([str(x) for x in test_result[3]])))