-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathevaluation.py
executable file
·65 lines (57 loc) · 1.83 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
#!/usr/bin/python
#coding=utf-8
from __future__ import print_function
import sys
import numpy as np
if sys.version_info.major == 2:
range = xrange
def eval_precision(U,V,TS,TN,VN,k):
num_user=U.shape[0]
TS_count=0
result_sum=0
num_item=V.shape[0]
print(num_user)
print(num_item)
for i in range(num_user):
idx_item=TN[0][i]
idx_item_ts=TS[0][i]
if len(idx_item_ts)==0 and len(VN[0][i])==0:
continue
TS_count+=1
item_candidate=set([ temp_i for temp_i in range(num_item)]) - set(idx_item)
R_i=U[i].dot(V[list(item_candidate)].T)
temp_R_i=list(zip(R_i,list(item_candidate)))
temp_R_i.sort(key=lambda x:x[0],reverse=True)
predicated=[x[1] for x in temp_R_i]
act_set=set(TS[0][i]) | set(VN[0][i])
pre_set=set(predicated[:k])
result=len(act_set & pre_set)/float(k)
result_sum+=result
print(result_sum)
print(TS_count)
precision_k=result_sum/TS_count
return precision_k
def eval_recall(U,V,TS,TN,VN,k):
num_user=U.shape[0]
num_item=V.shape[0]
TS_count=0
result_sum=0
for i in range(num_user):
idx_item=TN[0][i]
idx_item_ts=TS[0][i]
if len(idx_item_ts)==0 and len(VN[0][i])==0:
continue
TS_count+=1
item_candidate=set([ temp_i for temp_i in range(num_item)]) - set(idx_item)
R_i=U[i].dot(V[list(item_candidate)].T)
temp_R_i=list(zip(R_i,list(item_candidate)))
temp_R_i.sort(key=lambda x:x[0],reverse = True)
predicated=[x[1] for x in temp_R_i]
act_set=set(TS[0][i]) | set(VN[0][i])
pre_set=set(predicated[:k])
result=len(act_set & pre_set)/float(len(act_set))
result_sum+=result
print(result_sum)
print(TS_count)
recall_k=result_sum/TS_count
return recall_k