-
Notifications
You must be signed in to change notification settings - Fork 2
/
cur_90.py
219 lines (201 loc) · 7.34 KB
/
cur_90.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
214
215
216
217
import pandas as pd
import numpy as np
from numpy import linalg as LA
import math
import random
import time
num_of_users = 6040 + 1
num_of_movies= 3952 + 1
num_of_ratings = 1000209
precision_k = 5000
def main():
#Reading ratings file:
r_cols = ['user_id', 'movie_id', 'rating', 'unix_timestamp']
ratings = pd.read_csv('ml-1m/ratings.dat', sep="::", names=r_cols,encoding='latin-1',engine='python')
ratings = ratings[['user_id', 'movie_id', 'rating']]
ratings_list = ratings.values.tolist()
user_movie_matrix = np.zeros((num_of_users,num_of_movies))
#computing the utilty matrix
for i in range(num_of_ratings):
user_id = ratings_list[i][0]
movie_id = ratings_list[i][1]
rating = ratings_list[i][2]
user_movie_matrix[user_id][movie_id] = rating
matrix_centered_zero = np.copy(user_movie_matrix)
#centering the test data set
mean = 0.0
for i in range(1,num_of_users):
sum = 0.0
count = 0.0
#calculating mean
for j in range(1,num_of_movies):
if(user_movie_matrix[i][j] != 0):
sum = sum + user_movie_matrix[i][j]
count = count + 1.0
mean = sum / count
#centering the data about mean
for j in range(1,num_of_movies):
if(user_movie_matrix[i][j] == 0.0):
matrix_centered_zero[i][j] = mean
else:
matrix_centered_zero[i][j] = matrix_centered_zero[i][j] - mean
test = np.copy(matrix_centered_zero)
#making the training data set with the first 1000 * 1000 values as -1
for i in range(1,1001):
for j in range(1,1001):
if(matrix_centered_zero[i][j] != 0):
test[i][j] = -1
#center the training data set
mean = 0.0
for i in range(1,num_of_users):
sum = 0.0
count = 0.0
#calculating mean
for j in range(1,num_of_movies):
if(test[i][j] == -1):
sum = sum + 0.0
count = count + 1.0
elif(test[i][j] > 0):
sum = sum + test[i][j]
count = count + 1.0
mean = sum / count
#centering the data about mean
for j in range(1,num_of_movies):
if(test[i][j] == -1 or test[i][j] == 0):
test[i][j] = mean
else:
test[i][j] = test[i][j] - mean
#k factor for CUR
start = time.time()
k = 250
#computing the sum of all elements squared
total_sum_sq = 0.0
for i in range(1,num_of_users):
for j in range(1,num_of_movies):
total_sum_sq = total_sum_sq + (test[i][j])**2
#computing the probability distribution for all the columns
col_dis_pr = []
col_dis_pr.append(0.0)
for i in range(1,num_of_movies):
col_sum_sq = 0.0
for j in range(1,num_of_users):
col_sum_sq = col_sum_sq + (test[j][i])**2
col_dis_pr.append(col_sum_sq / total_sum_sq)
#computing the probabilty distribution for all the rows
row_dis_pr = []
row_dis_pr.append(0.0)
for i in range(1,num_of_users):
row_sum_sq = 0.0
for j in range(1,num_of_movies):
row_sum_sq = row_sum_sq + (test[i][j])**2
row_dis_pr.append(row_sum_sq / total_sum_sq)
#computing a list with indices of all columns
cols_index = []
cols_index.append(-1)
for i in range(0,3952):
cols_index.append(i+1)
#computing a list with indices of all rows
rows_index = []
rows_index.append(-1)
for i in range(0,6040):
rows_index.append(i+1)
#computing random values with given probability distribution
cols = np.random.choice(cols_index, 4 * k,replace=False, p = col_dis_pr)
rows = np.random.choice(rows_index, 4 * k,replace=False, p = row_dis_pr)
#c = 4 * k
c_attr = 1000.0
C = np.zeros((num_of_users, 4*k + 1))
#Computing C
for i in range(1,4*k+1):
C[:,i] = np.divide(test[:,cols[i-1]], np.sqrt(np.multiply(c_attr,col_dis_pr[cols[i-1]])))
R = np.zeros((4*k+1, num_of_movies))
#Computing R
for i in range(1,4*k+1):
R[i,:] = np.divide(test[rows[i-1],:], np.sqrt(np.multiply(c_attr,row_dis_pr[rows[i-1]])))
#Computing their pseudoinverses
C_inv = np.linalg.pinv(C)
R_inv = np.linalg.pinv(R)
#Computing U
U = np.matmul(np.matmul(C_inv, test), R_inv)
#Computing SVD of U
AtA = np.dot(np.transpose(U), U)
#Computing eigen values and vectors
eigen_values_V , eigen_vectors_V = LA.eig(AtA)
#discarding the imaginary part of eigen vectors and values
eigen_values_V = np.real(eigen_values_V)
eigen_vectors_V = np.real(eigen_vectors_V)
#sorting the eigen values in descending order
idV = np.argsort(np.multiply(-1,eigen_values_V))
eigen_values_V = eigen_values_V[idV]
#rearranging eigen vectors with respect to eigen values
eigen_vectors_V = eigen_vectors_V[:, idV]
#computing the sigma matrix
S = np.sqrt(np.abs(eigen_values_V))
S = np.diag(S)
#computing the inverse of the sigma matrix
Sinv = np.linalg.pinv(S)
#computing the left SVD matrix
W = np.matmul(np.matmul(U, eigen_vectors_V), Sinv)
#Computing the 90% sigma matrix
energy = 0.0
#computing the total energy
for i in range(S.shape[0]):
energy = energy + S[i][i]**2
#computing 90% of total energy
ninety_percent = 0.9 * energy
cut = 0
while(energy > ninety_percent):
temp_energy = 0.0
#compute leaving the last 'cut' elements
for i in range(S.shape[0] - cut):
temp_energy = temp_energy + S[i][i] ** 2
#check if its more than 90%
if(temp_energy > ninety_percent):
#if yes then update cut
cut = cut + 1
continue
else:
break
size_S = S.shape[0]
new_shape = size_S - cut + 1
#reshape all the matrices
new_S = S[0:new_shape,0:new_shape]
new_U = W[:,0:new_shape]
R_new = eigen_vectors_V[0:new_shape, :]
#Compute the U in CUR
answer = np.matmul(np.matmul(new_U, new_S), (R_new))
#Compute the original matrix
answer = np.matmul(np.matmul(C, answer), R)
squares_sum = 0.0
count_sq = 0.0
precision_rating = []
for i in range(1,1001):
for j in range(1,1001):
if(user_movie_matrix[i][j] != 0):
precision_rating.append(answer[i][j])
print("Actual rating")
print(matrix_centered_zero[i][j])
print("Predicted rating")
print(answer[i][j])
#compute rmse
squares_sum = squares_sum + (answer[i][j] - matrix_centered_zero[i][j])**2
count_sq = count_sq + 1.0
print("")
print("Root mean squared error")
print(math.sqrt(squares_sum / count_sq))
print("Spearman's correlation")
correlation = 1 - ((6 * squares_sum) / (count_sq**3 - count_sq))
print(correlation)
#calculation of the precision at top k
precision_rating.sort(reverse=True)
countk = 0.0
for i in range(0, precision_k):
if(precision_rating[i] >= 1.0):
countk = countk + 1
precision_at_topk = countk / precision_k
print("Precision at top k")
print(precision_at_topk)
print("Time required for collaborative filtering ")
print("--- %s seconds ---" % (time.time() - start))
if __name__ == "__main__":
main()