-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollaborative_filtering.py
211 lines (181 loc) · 5.58 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
203
204
205
206
207
208
209
210
211
from surprise import Reader, Dataset, KNNBaseline
import pandas as pd
from file_paths import *
from content_based import ContentBased
from numpy import array
from os.path import isfile
import joblib
DEFAULT_ID = 700
RATING_ATTR = ["userId", "movieId", "rating"]
LIMIT = 20
class CollaborativeFiltering:
def __init__(self):
self.df_credits = pd.read_csv(PATH_CREDITS)
self.m_id_to_tmdb = pd.read_csv(PATH_MOVIELENS_TO_TMDB)
self.df_movies = pd.read_csv(PATH_MOVIES)
def get_tmdb_id(self, movieId):
"""
param: movieId - movieId of the movie (as seen in MovieLens dataset)
return: tmdbId corresponding to the movieID
"""
try:
return self.m_id_to_tmdb["tmdbId"][
self.m_id_to_tmdb.index[self.m_id_to_tmdb["movieId"] == movieId][0]
]
except:
pass
def get_m_id(self, tmdbId):
"""
param: tmdbId - tmdbId of the movie (as seen in TMDB dataset)
return: movieId corresponding to the tmdbId
"""
try:
return self.m_id_to_tmdb["movieId"][
self.m_id_to_tmdb.index[self.m_id_to_tmdb["tmdbId"] == tmdbId][0]
]
except:
pass
def get_title_index(self, title):
"""
param: title - movie title (as in TMDB dataset)
return: if title found - returns index value of the title from df_credits
"""
try:
return self.df_credits["id"][
self.df_credits.index[self.df_credits["title"] == title][0]
]
except:
raise ValueError("No film : " + title + " found!")
def get_movie_title(self, m_id):
"""
param: m_id - index value (id) (corresponding to df_movies)
return: movie title corresponding to m_id (as in df_movies)
"""
try:
vals = self.df_movies.iloc[
self.df_movies.index[self.df_movies["id"] == m_id][0]
][
[
"title",
"id",
"vote_average",
"vote_count",
"popularity",
"release_date",
]
].values
return vals
except:
return None
def get_movie_ids(self):
"""
param: None
return: List of all the unique movieIds (from PATH_RATINGS path)
"""
return pd.read_csv(PATH_RATINGS)["movieId"].unique().tolist()
def train_knn(self, df, userId, user_m_ids, movies_watched):
"""
param: df - movies pandas DataFrame
userId - user ID to predict movies with
user_m_ids - List of movieIDs of movies to be recommended upon
(as seen in TMDB dataset)
movies_watched - List of movie titles watched
(as seen in TMDB dataset)
return: pandas DataFrame of the recommended movies with attributes -
title, id, vote_average, vote_count, popularity, release date
Collaborative filtering is done using KNN-Baseline and prediction is
done using pearson_baseline. The technique used is item-item based.
"""
reader = Reader(rating_scale=(1, 5))
movie_ids = self.get_movie_ids()
rec_result = dict()
sim_options = {"name": "pearson_baseline", "user_based": False}
data = Dataset.load_from_df(df[RATING_ATTR], reader)
if isfile(PATH_COLL_FILTERING_CACHE):
model = joblib.load(PATH_COLL_FILTERING_CACHE)
else:
trainset = data.build_full_trainset()
model = KNNBaseline(sim_options=sim_options)
model.fit(trainset)
joblib.dump(model, PATH_COLL_FILTERING_CACHE)
inn_id = model.trainset.to_inner_iid(user_m_ids[0])
# print(self.get_movie_title(self.get_tmdb_id(user_m_ids[0])))
inn_id_neigh = model.get_neighbors(inn_id, k=10)
# print(inn_id_neigh)
df_pref = pd.DataFrame(
columns=[
"title",
"id",
"vote_average",
"vote_count",
"popularity",
"release_date",
]
)
index = 0
for m_id in inn_id_neigh:
title_df = self.get_movie_title(
self.get_tmdb_id(model.trainset.to_raw_iid(m_id))
)
try:
if title_df[0] not in movies_watched:
df_pref.loc[index] = array(
[
title_df[0],
title_df[1],
title_df[2],
title_df[3],
title_df[4],
title_df[5],
]
)
index += 1
except:
pass
return df_pref
def store_pref(self, pref, userId, index, rating):
"""
param: pref - dict with keys - userId, movieId, rating
userId - userId to be inserted (int)
index - movieId to be inserted (int)
rating - rating on a scale of (1-5)
return: pref with userId, index and rating values appended
"""
pref[RATING_ATTR[0]].append(userId)
pref[RATING_ATTR[1]].append(index)
pref[RATING_ATTR[2]].append(rating)
return pref
def user_model(self, movies_watched, limit=LIMIT):
"""
param: movies_watched - dict of form -
{'movieName1': rating1,
'movieName2': rating2,
.....}
limit - no. of movies to display
(default = LIMIT)
"""
LIMIT = limit
userId = DEFAULT_ID
pref = dict()
user_m_ids = list()
for attribute in RATING_ATTR:
pref[attribute] = list()
for title in movies_watched:
index = self.get_title_index(title)
m_index = self.get_m_id(index)
user_m_ids.append(m_index)
pref = self.store_pref(pref, userId, m_index, movies_watched[title])
df_rating = pd.read_csv(PATH_RATINGS)
df_pref_rating = df_rating.append(
pd.DataFrame(pref), sort=False, ignore_index=True
)
return self.train_knn(df_pref_rating, userId, user_m_ids, movies_watched)
if __name__ == "__main__":
rec = CollaborativeFiltering()
print(
rec.user_model(
{
"Apollo 13": 5
}
)
)