-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevaluation.py
45 lines (34 loc) · 1.02 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
import math
import numpy as np
def precision(actual, predicted, N):
if isinstance(N, int):
inter_set = set(actual) & set(predicted[:N])
return float(len(inter_set))/float(N)
elif isinstance(N, list):
return np.array([precision(actual, predicted, n) for n in N])
def recall(actual, predicted, N):
if isinstance(N, int):
inter_set = set(actual) & set(predicted[:N])
return float(len(inter_set))/float(len(set(actual)))
elif isinstance(N, list):
return np.array([recall(actual, predicted, n) for n in N])
def nDCG(Tr, topK, num=None):
if num is None:
num = len(topK)
dcg, vec = 0, []
for i in xrange(num):
if topK[i] in Tr:
dcg += 1/math.log(i+2, 2)
vec.append(1)
else:
vec.append(0)
vec.sort(reverse=True)
idcg = sum([vec[i]/math.log(i+2, 2) for i in xrange(num)])
if idcg > 0:
return dcg/idcg
else:
return idcg
def mean_average_precision():
pass
def AUC():
pass