-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
204 lines (142 loc) · 4.52 KB
/
example.py
File metadata and controls
204 lines (142 loc) · 4.52 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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
from src.algorithms.alternating_least_squares import AlternatingLeastSquares
from src.helpers.dataset_indexer import DatasetIndexer
from src.helpers.checkpoint_manager import CheckpointManager
from src.recommenders import CollaborativeFilteringRecommenderBuilder
from src.backends import Backend
from src.helpers._logging import logger # noqa
from src.settings import settings
from src.utils import vocabulary_based_one_hot_encode
from src.helpers.graphing import (
plot_als_train_test_loss_evolution,
plot_als_train_test_rmse_evolution,
# plot_error_evolution,
plot_power_low_distribution,
plot_data_item_distribution_as_hist,
)
# In[2]:
USER_HEADER = "userId"
ITEM_HEADER = "movieId"
RATING_HEADER = "rating"
FEATURE_TO_ENCODE = "genres"
ITEM_FEATURE_LIST = [
"Action",
"Adventure",
"Animation",
"Children",
"Comedy",
"Crime",
"Documentary",
"Drama",
"Fantasy",
"Film-Noir",
"Horror",
"IMAX",
"Musical",
"Mystery",
"Romance",
"Sci-Fi",
"Thriller",
"War",
"Western",
]
# In[3]:
dataset_indexer = DatasetIndexer(
file_path="./ml-32m/ratings.csv",
user_header=USER_HEADER,
item_header=ITEM_HEADER,
rating_header=RATING_HEADER,
limit=settings.general.LINES_COUNT_TO_READ,
)
indexed_data = dataset_indexer.index_simple(
approximate_train_ratio=settings.general.APPROXIMATE_TRAIN_RATIO
)
# In[4]:
# Import the movies csv file joined with the movie links csv file and that will act
# as our movie database. The backend needs this database to query the movies.
item_database = (
pd.read_csv("./ml-32m/movies.csv", dtype={ITEM_HEADER: str})
.merge(
pd.read_csv("./ml-32m/links.csv", dtype={ITEM_HEADER: str}),
on=ITEM_HEADER,
how="left",
)
.assign(
genres=lambda df: df[FEATURE_TO_ENCODE].apply(lambda genres: genres.split("|")),
features_hot_encoded=lambda df: df[FEATURE_TO_ENCODE].apply(
lambda g: vocabulary_based_one_hot_encode(
words=g, vocabulary=ITEM_FEATURE_LIST
)
),
features_count=lambda df: df["features_hot_encoded"].apply(lambda x: sum(x)),
)
.set_index(ITEM_HEADER) # Set the movieId as the index
.to_dict(orient="index") # Convert the DataFrame to a dictionary
)
# In[5]:
# plot_data_item_distribution_as_hist(indexed_data)
# In[6]:
# plot_power_low_distribution(indexed_data,)
# In[7]:
als_instance = AlternatingLeastSquares(
hyper_lambda=settings.als.HYPER_LAMBDA,
hyper_gamma=settings.als.HYPER_GAMMA,
hyper_tau=settings.als.HYPER_TAU,
hyper_n_epochs=settings.als.HYPER_N_EPOCH,
hyper_n_factors=settings.als.HYPER_N_FACTOR,
)
als_backend = Backend(
# Define the algorithm
algorithm=als_instance,
checkpoint_manager=CheckpointManager(
checkpoint_folder=settings.als.CHECKPOINT_FOLDER,
sub_folder=str(settings.general.LINES_COUNT_TO_READ),
),
# The predictor needs this to render the name of the items
item_database=item_database,
# Whether we should resume by using the last state of
# the algorithm the checkpoint manager folder or not.
resume=False,
save_checkpoint=True,
)
# In[8]:
recommender_builder = CollaborativeFilteringRecommenderBuilder(
backend=als_backend,
)
# This might take some moment before finishing
recommender = recommender_builder.build(
data=indexed_data, item_database=item_database, include_features=True
)
# In[ ]:
# In[9]:
# plot_als_train_test_rmse_evolution(als_backend.algorithm)
# In[10]:
# plot_als_train_test_loss_evolution(als_backend.algorithm)
# 279178
# In[11]:
#
prediction_input = [("17", 5)] # Sense and Sensibility (1995)
predictions = recommender.recommend(prediction_input)
logger.info(
f'Prediction for Sense and Sensibility (1995) (id=17) is {[p["title"] + ": " + "|".join(p["genres"]) for p in predictions]}'
)
# In[12]:
prediction_input = [("267654", 5)] # Harry Poter
predictions = recommender.recommend(prediction_input)
logger.info(
f'Prediction for Harry Poter (id=267654) is {[p["title"] + ": " + "|".join(p["genres"]) for p in predictions]}'
)
# In[13]:
prediction_input = [("279178", 5)] # Lord of the ring
predictions = recommender.recommend(prediction_input)
logger.info(
f'Prediction for Lord of the ring (id=279178) is {[p["title"] + ": " + "|".join(p["genres"]) for p in predictions]}'
)
# In[14]:
# Trends prediction
# This needs a lot of RAMS
# recommender.recommend()
# %%