forked from vam-sin/Recommender-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collaborative_filtering.py
202 lines (174 loc) · 6.89 KB
/
collaborative_filtering.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
import copy
import csv
import heapq
import numpy as np
import time
"""
File to perform collaborative filtering on data with and without baseline approach
"""
NUM_MOVIES = 3952
NUM_USERS = 6040
PRINT_FREQUENCY = 100
def create_utility_matrix():
# read from file and make utility matrix
utility_matrix = np.zeros((NUM_USERS, NUM_MOVIES), np.float32)
with open('ml-1m/ratings.dat', 'r') as datafile:
dataReader = csv.DictReader(datafile, delimiter=':')
i = 0
for row in dataReader:
if i%PRINT_FREQUENCY == 0:
print("[Utility Matrix - Input] Rating# ", i)
for key in row:
row[key] = int(row[key])
utility_matrix[row['UserID']-1][row['MovieID']-1] = row['Rating']
i+=1
print("Inputted total of ", i, " ratings from dataset")
return utility_matrix
def rmse(arr1, arr2):
'''
calculate root mean square error
'''
return (np.square(arr1 - arr2).mean()**0.5)
def mae(arr1, arr2):
'''
calculates mean absolute error
'''
return np.abs(arr1 - arr2).mean()
def predict_rating_vanilla(user, item, neighbours):
'''
predict ratings of unrated movies using plain collaborative filtering
'''
global user_average_ratings, cosine_similarities, normalized_utility_matrix
rating = 0
for neighbour in neighbours:
rating += (cosine_similarities[user][neighbour] * normalized_utility_matrix[neighbour][item])
rating /= len(neighbours)
rating += user_average_ratings[user]
return rating
def predict_rating_baseline(user, item, neighbours):
'''
If baseline is True, the baseline value b is calculated as:
b = mu + bx + bi
where:
mu = global average
bx = (user_average - mu)
bi = (item_average - mu) (item -> movie)
'''
global user_average_ratings, movie_average_ratings, cosine_similarities, utility_matrix, global_movie_average_rating
mu = global_movie_average_rating
bx = user_average_ratings[user] - mu
bi = movie_average_ratings[item] - mu
b = mu + bx + bi
numerator = 0
denominator = 0
for neighbour in neighbours:
'''
utility_matrix[neighbour][item] - (mu + bi + user_average_ratings[neighbour] - mu)
'''
value = utility_matrix[neighbour][item] - (bi + user_average_ratings[neighbour])
numerator += cosine_similarities[user][neighbour] * value
denominator += cosine_similarities[user][neighbour]
assert(type(numerator) == np.float64)
assert(type(numerator) == type(denominator))
if denominator < 0.000001:
return 0
rating = numerator/denominator
rating += b
return rating
def knn(user, k=2):
'''
Find the k-nearest neighbours of user using cosine/pearson similarities
'''
global cosine_similarities
neighbours = heapq.nlargest(k+1, range(cosine_similarities.shape[0]), cosine_similarities[user].__getitem__)
if user in neighbours:
# user cannot be a neighbour of themselves
np.delete(neighbours, np.where(neighbours == user))
else:
np.delete(neighbours, k)
return neighbours
def create_prediction_matrix(utility_matrix, baseline, k=2):
'''
create a matrix of predictions
'''
assert(utility_matrix.shape == (NUM_USERS, NUM_MOVIES))
prediction_matrix = np.zeros(utility_matrix.shape, np.float32)
for i in range(0, NUM_USERS):
if i%PRINT_FREQUENCY == 0:
if baseline:
print("[Predictions - Baseline] ", end='')
else:
print("[Predictions - Vanilla] ", end='')
print("Processed ", i, " users so far...")
neighbours = knn(i, k)
for j in range(0, NUM_MOVIES):
if not baseline:
prediction_matrix[i][j] = predict_rating_vanilla(i, j, neighbours)
else:
prediction_matrix[i][j] = predict_rating_baseline(i, j, neighbours)
return prediction_matrix
#### EXECUTION STARTS HERE
start_time = time.process_time()
# read from dataset
utility_matrix = create_utility_matrix()
try:
with open('global_movie_average_rating.csv','r') as datafile:
data_iter = csv.reader(datafile, delimiter = ',')
data = [data for data in data_iter]
global_movie_average_rating = np.asarray(data, dtype = np.float32)[0][0]
with open('movie_average_ratings.csv','r') as datafile:
data_iter = csv.reader(datafile, delimiter = ',')
data = [data for data in data_iter]
movie_average_ratings = np.asarray(data, dtype = np.float32).flatten()
print(movie_average_ratings)
with open('normalized_utility_matrix.csv','r') as datafile:
data_iter = csv.reader(datafile, delimiter = ',')
data = [data for data in data_iter]
normalized_utility_matrix = np.asarray(data, dtype = np.float32)
print(normalized_utility_matrix)
with open('user_average_ratings.csv','r') as datafile:
data_iter = csv.reader(datafile, delimiter = ',')
data = [data for data in data_iter]
user_average_ratings = np.asarray(data, dtype = np.float32).flatten()
print(user_average_ratings)
with open('user_similarities.csv','r') as datafile:
data_iter = csv.reader(datafile, delimiter = ',')
data = [data for data in data_iter]
cosine_similarities = np.asarray(data, dtype = np.float32)
print(cosine_similarities)
del data_iter, data
except FileNotFoundError:
print("user_similarities.csv is needed to run this file. Run preprocess.py to generate file.")
print("Exiting...")
exit()
read_time = time.process_time()
print("Starting vanilla filtering...")
vanilla_collaborative_filtering = create_prediction_matrix(utility_matrix, baseline = False)
vanilla_time = time.process_time()
print("Starting baseline filtering... Vanilla filtering took ", vanilla_time- read_time, " seconds")
baseline_collaborative_filtering = create_prediction_matrix(utility_matrix, baseline = True)
print(baseline_collaborative_filtering)
baseline_time = time.process_time()
print("Calculating RMSE")
rmse_vanilla_collaborative = rmse(vanilla_collaborative_filtering, utility_matrix)
mae_vanilla_collaborative = mae(vanilla_collaborative_filtering, utility_matrix)
vanilla_error_time = time.process_time()
rmse_baseline_collaborative = rmse(baseline_collaborative_filtering, utility_matrix)
mae_baseline_collaborative = mae(baseline_collaborative_filtering, utility_matrix)
baseline_error_time = time.process_time()
print("\t\t=== Collaborative Filtering ===")
print("\tRMSE: ", rmse_vanilla_collaborative)
print("\tMAE: ", mae_vanilla_collaborative)
print("\tPrediction time: ", vanilla_time - read_time)
print("\tError calc time: ", vanilla_error_time - baseline_time)
print("\t\t=== Baseline Collaborative Filtering ===")
print("\tRMSE: ", rmse_baseline_collaborative)
print("\tMAE: ", mae_baseline_collaborative)
print("\tPrediction time: ", baseline_time - vanilla_time)
print("\tError calc time: ", baseline_error_time - vanilla_error_time)
np.savetxt("vanilla_collaborative_filtering.csv", vanilla_collaborative_filtering, fmt = "%5.5f", delimiter=",")
np.savetxt("baseline_collaborative_filtering.csv", baseline_collaborative_filtering, fmt = "%5.5f", delimiter=",")
file_output_time = time.process_time()
print("\n\n\t\tFILE INPUT TIME: ", read_time - start_time)
print("\n\n\t\tFILE OUTPUT TIME: ", file_output_time - baseline_error_time)
print("\t\tTOTAL EXECUTION TIME: ", file_output_time - start_time)