-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathridge.py
213 lines (178 loc) · 6.52 KB
/
ridge.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
import numpy as np
import util
import matplotlib.pyplot as plt
import pickle
class Ridge():
'''Weighted Ridge Regression'''
def __init__(self, f = 200, alpha=20, lambd = 0.1, thres = 0 ,epsilon = 0.1):
self.f = f
self.alpha = alpha
self.lambd = lambd
self.epsilon = epsilon
# Input: Rating matrix A
# Hyperparameters: Vector Space Dimension: f ; alpha ; lambd
def fit(self, A):
k = len(A) #number of users
n = len(A[0]) #number of movies
# Construct the Preference matrix P
# Construct the Confidence matrix C
self.P = np.zeros((k,n))
C = np.zeros((k,n))
for u in range(k): # users
for m in range(n): # movies
if A[u][m]>thres :
self.P[u][m] = 1
else :
self.P[u][m] = 0
C[u][m] = 1 + self.alpha*A[u][m]
# print("P and C built")
# Construct the C^u matrices and the C^m matrices
Cu = []
Cm = []
for u in range(k):
Cu.append(np.diag(C[u]))
for m in range(n):
Cm.append(np.diag(C[:,m]))
#print("Cu and Cm built")
# Initialize X
self.X = np.random.rand(k,self.f)
Xprev = self.X+2*self.epsilon*np.ones((k,self.f))
# Initialize Y
self.Y = np.random.rand(self.f,n)
Yprev = self.Y+2*self.epsilon*np.ones((self.f,n))
while max(np.linalg.norm(self.X-Xprev) , np.linalg.norm(self.Y-Yprev) ) > self.epsilon:
Xprev = self.X
Yprev = self.Y
for u in range(k): # users
self.X[u] = np.transpose(np.dot( np.linalg.inv(np.dot(np.dot( self.Y , Cu[u]) , np.transpose(self.Y) ) + self.lambd*np.identity(self.f)) , np.dot(np.dot(self.Y, Cu[u]),self.P[u] )))
for m in range(n): # movies
self.Y[:,m] = np.dot( np.linalg.inv(np.dot(np.dot( np.transpose(self.X) , Cm[m]) , self.X ) + self.lambd*np.identity(self.f)) , np.dot(np.dot(np.transpose(self.X), Cm[m]),self.P[:,m] ))
#print("fitting done")
# K is the number of recommended movies
def predict(self, u, K = 9125):
#def predict(self, u, K = 100):
P_u_hat = np.dot(self.X[u] , self.Y)
indices = np.argsort(P_u_hat)
# Recommended movies that have not been rated yet
# k=0
# i = 0
# recommended_movies = []
# while k < K and i < len(indices) :
# if self.P[u][indices[i]] == 0 :
# k += 1
# recommended_movies.append(indices[i])
# i += 1
recommended_movies = indices[:K].tolist()
return recommended_movies
def rank(mat1, r):
k = len(mat1) #number of users
n = len(mat1[0]) #number of movies
sum_numerator = 0
sum_denominator = np.sum(mat1)
for u in range(k):
recommendations = r.predict(u)
K = len(recommendations)
rank_u = np.zeros(n)
for m in range(n):
if m in recommendations :
rank_u[m] = recommendations.index(m)/(K-1)
for m in range(n):
sum_numerator += mat1[u,m]*rank_u[m]
return(sum_numerator / sum_denominator)
if __name__ == "__main__":
'''Basic test of the algorithm'''
A = util.load_data_matrix()
# A = util.load_data_matrix()[:,:100] # if tested on a laptop, please use the first 100 movies
print(A, A.shape)
r = Ridge()
r.fit(A)
recommendations = r.predict(1) # predicts the top K movies for user 1
print(recommendations)
B = pickle.load( open('{}'.format('data/data_dicts.p'), 'rb'))
for movie_id,rating in B['userId_rating'][2]:
if rating ==5 :
print(B['movieId_movieName'][movie_id] , ", rating:" , rating )
l = recommendations
k_list =[]
for movie_column in l :
for k, v in B['movieId_movieCol'].items():
if v == movie_column:
k_list.append(k)
print("")
print("Recommendations")
for movie_id in k_list :
print(B['movieId_movieName'][movie_id])
'''Choice of hyperparameters'''
A = util.load_data_matrix()
# A = util.load_data_matrix()[:,:100] # if tested on a laptop, please use the first 100 movies
f_range = np.arange(100,400,20)
ranks_f = []
alpha_range = np.arange(10, 80, 10)
ranks_alpha = []
lambd_range = np.logspace(-1, 1, 10)
ranks_lambd = []
thres_range = np.arange(0, 3.5, 0.5)
ranks_thres = []
k = 4
train_mats, val_mats, masks = util.k_cross(k=k)
'''Choice of f'''
for f in f_range :
print(f)
x=[]
for i in range(k):
train_mat = train_mats[i]
val_mat = val_mats[i]
r = Ridge(f=f)
r.fit(train_mat)
x.append(rank(val_mat, r))
ranks_f.append(np.mean(x)*100)
plt.plot(f_range,ranks_f)
plt.ylabel('expected percentile ranking (%)')
plt.xlabel('f')
plt.show()
'''Choice of alpha'''
for alpha in alpha_range :
print(alpha)
x=[]
for i in range(k):
train_mat = train_mats[i]
val_mat = val_mats[i]
r = Ridge(alpha=alpha)
r.fit(train_mat)
x.append(rank(val_mat, r))
ranks_alpha.append(np.mean(x)*100)
plt.plot(alpha_range,ranks_alpha)
plt.ylabel('expected percentile ranking (%)')
plt.xlabel('alpha')
plt.show()
'''Choice of lambda'''
for lambd in lambd_range :
print(lambd)
x=[]
for i in range(k):
train_mat = train_mats[i]
val_mat = val_mats[i]
r = Ridge(lambd=lambd)
r.fit(train_mat)
x.append(rank(val_mat, r))
ranks_lambd.append(np.mean(x)*100)
plt.semilogx(lambd_range,ranks_lambd)
plt.ylabel('expected percentile ranking (%)')
plt.xlabel('lambda')
plt.show()
'''Choice of threshold'''
for thres in thres_range :
print(thres)
x=[]
for i in range(k):
train_mat = train_mats[i]
val_mat = val_mats[i]
r = Ridge(thres=thres)
r.fit(train_mat)
x.append(rank(val_mat, r))
ranks_thres.append(np.mean(x)*100)
plt.plot(thres_range,ranks_thres)
plt.ylabel('expected percentile ranking (%)')
plt.xlabel('threshold')
plt.show()