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.py
More file actions
123 lines (107 loc) · 4.04 KB
/
Copy pathcollab_filter.py
File metadata and controls
123 lines (107 loc) · 4.04 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
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 Collab_Filter:
"""
A Recommender System model based on the Collaborative filtering concepts.
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.
"""
def __init__(self, path):
"""
Normalizes the Utility matrix consisting of users, movies and their ratings
by subtracting values in a row by their row mean, which handles the problem
of strict and generous raters.
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))
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
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
# 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 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]]
den += x[0]
r = num / den
r = r + self.user_mean[u]
return r
if __name__ == "__main__":
cf = Collab_Filter("./data/utility_matrix.csv")
# for x in cf.top_sim_items(1,1):
# print(x,cf.movies[x[1]])
print("Rating: ", cf.predict_rating(970, 2194))
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(cf.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))