-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcal_precision.py
30 lines (28 loc) · 1007 Bytes
/
cal_precision.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
import numpy as np
from data import read_user
def cal_precision(p,cut):
R_true = read_user('cf-test-1-users.dat')
dir_save = 'cdl'+str(p)
U = np.mat(np.loadtxt(dir_save+'/final-U.dat'))
V = np.mat(np.loadtxt(dir_save+'/final-V.dat'))
R = U*V.T
num_u = R.shape[0]
num_hit = 0
fp = open(dir_save+'/rec-list.dat','w')
for i in range(num_u):
if i!=0 and i%100==0:
print 'Iter '+str(i)+':'+str(float(num_hit)/i/cut)
l_score = R[i,:].A1.tolist()
pl = sorted(enumerate(l_score),key=lambda d:d[1],reverse=True)
l_rec = list(zip(*pl)[0])[:cut]
s_rec = set(l_rec)
#_true = set(np.where(R_true[i,:]>0)[1].A1)
s_true = set(np.where(R_true[i,:]>0)[1])
cnt_hit = len(s_rec.intersection(s_true))
num_hit += cnt_hit
fp.write('%d:' % cnt_hit)
fp.write(' '.join(map(str,l_rec)))
fp.write('\n')
fp.close()
print 'Precision: %.3f' % (float(num_hit)/num_u/cut)
cal_precision(2,8)