This repository was archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollab_filter_baseline.py
More file actions
149 lines (130 loc) · 5.11 KB
/
collab_filter_baseline.py
File metadata and controls
149 lines (130 loc) · 5.11 KB
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
import numpy as np
import pandas as pd
import math
from numpy.linalg import norm
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from math import sqrt
class CF_Baseline:
"""
A Recommender System model based on the Collaborative filtering concepts with ceratin enhancements.
An Item-Item based collaborative filtering is used to find similar items which then is used to
predict rating a user might give to a movie/item based on the ratings he gave to similar items.
Also calculates rating deviations of users to the form of the mean of ratings to handle strict and generous raters.
The result is enhanced by computing baseline estimates for ratings to be predicted.
"""
def __init__(self, path):
"""
Pre-processes the Utility matrix consisting of users, movies and their ratings to contain only numbers
Also computed eac user's and item's ratings mean which will be used to calculate baseline estimates.
Args:
path (string) : The path to the csv which stores the utility matrix.
"""
self.path = path
self.umDf = pd.read_csv(path, low_memory=False)
self.user_mean = [0 for j in range(6041)]
self.um = self.umDf.values
self.movies = list(self.umDf.columns)
# print(len(self.movies))
self.movie_mean = [0 for j in range(len(self.movies))]
self.global_mean = 0
self.global_count = 0
for i in range(1, 6041):
m = 0
cnt = 0
for j in range(1, len(self.movies)):
if not math.isnan(self.um[i][j]):
m = m + self.um[i][j]
cnt += 1
self.user_mean[i] = m / cnt
self.global_mean += m
self.global_count += cnt
for j in range(1, len(self.movies)):
if not math.isnan(self.um[i][j]):
self.um[i][j] = self.um[i][j] # -self.user_mean[i]
else:
self.um[i][j] = 0
self.global_mean = self.global_mean / self.global_count
for j in range(1, len(self.movies)):
m = 0
cnt = 0
for i in range(1, 6041):
if self.um[i][j] != 0:
m = m + self.um[i][j]
cnt = cnt + 1
if cnt != 0:
self.movie_mean[j] = m / cnt
# print(self.um[1][:],self.user_mean[1])
self.um = np.array(self.um)
def item_sim(self, i, j):
"""
Calculates similarity between two items/movies
Args:
i (int) : Column number of first movie
j (int) : Column number of second movie
Returns:
The similarity value between the two items
"""
return np.dot(self.um[1:, i], self.um[1:, j]) / (
norm(self.um[1:, i]) * norm(self.um[1:, j])
)
def top_sim_items(self, u, i):
"""
Finds the items most similar to given item , which are rated by the user
Args:
u (int) : User's ID
i (int) : Column number/movie_id of required item
Returns:
list : A list of movie_ids of movies similar to given movie and their similarity values
"""
ti = []
for j in range(1, len(self.movies)):
if j != i and self.um[u][j] != 0:
ti.append((self.item_sim(i, j), j))
ti.sort(reverse=True)
return ti[:15]
def baseline(self, u, m):
"""
Computes a baseline estimate for a given user and movie
Args:
u (int) : User's ID
m (int) : movie_id of the required movie/item
Returns:
The baseline estimate of a rating for a give user and movie
"""
return self.user_mean[u] + self.movie_mean[m] - self.global_mean
def predict_rating(self, u, m):
"""
Predicts the rating a user might give to a movie
Args:
u (int) : User's ID
m (int) : movie_id of the required movie/item
Returns:
The predicted rating user u might give to movie m
"""
m = self.movies.index(str(m))
ti = self.top_sim_items(u, m)
num, den = 0, 0
for x in ti:
num += x[0] * (self.um[u][x[1]] - self.baseline(u, x[1]))
den += x[0]
r = num / den
r = r + self.baseline(u, m)
return r
if __name__ == "__main__":
cfb = CF_Baseline("./data/utility_matrix.csv")
# for x in cf.top_sim_items(1,1):
# print(x,cf.movies[x[1]])
print("Rating: ", cfb.predict_rating(3589, 1562))
test_df = pd.read_csv("./data/test_ratings.csv")
u_id = test_df["user_id"].values
m_id = test_df["movie_id"].values
Y = test_df["rating"].values
predictions = []
for i in range(15000):
predictions.append(cfb.predict_rating(u_id[i], m_id[i]))
if i % 200 == 0 and i != 0:
print(
"RMSE at ", i, " :", sqrt(mean_squared_error(Y[: i + 1], predictions))
)
print("MAE at ", i, " :", mean_absolute_error(Y[: i + 1], predictions))