-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.py
41 lines (30 loc) · 909 Bytes
/
util.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
'''
Created on July 14, 2017
@author: Beili
'''
import sys
import numpy as np
if sys.version_info.major == 2:
range = xrange
def eval_RMSE(R, U, V, TS):
num_user = U.shape[0]
sub_rmse = np.zeros(num_user)
TS_count = 0
for i in range(num_user):
idx_item = TS[i]
if len(idx_item) == 0:
continue
TS_count = TS_count + len(idx_item)
approx_R_i = U[i].dot(V[idx_item].T) # approx_R[i, idx_item]
R_i = R[i]
sub_rmse[i] = np.square(approx_R_i - R_i).sum()
rmse = np.sqrt(sub_rmse.sum() / TS_count)
return rmse
def make_CDL_format(X_base, path):
max_X = X_base.max(1).toarray()
for i in range(max_X.shape[0]):
if max_X[i, 0] == 0:
max_X[i, 0] = 1
max_X_rep = np.tile(max_X, (1, X_base.shape[1]))
X_nor = X_base / max_X_rep
np.savetxt(path + '/mult_nor.dat', X_nor, fmt='%.5f')