-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
408 lines (300 loc) · 12.3 KB
/
utils.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import csv
import numpy as np
import pandas as pd
def read_txt(path):
"""read text file from path."""
with open(path, "r") as f:
return f.read().splitlines()
def deal_line(line):
"""
Return the index of the row and column form a csv line, by also removing 1 (so that the indices start at 0
----------
line : String
A line of th format of the provided csv file
Returns:
The index of the row and column subtracted by 1
"""
pos, rating = line.split(',')
row, col = pos.split("_")
row = row.replace("r", "")
col = col.replace("c", "")
return int(row)-1, int(col)-1 #remove one to have same indices
def predict_on_model_line(data):
"""preprocessing the text data, conversion to numerical array format."""
data = [deal_line(line) for line in data]
return data
def create_csv_submission(ids, y_pred, name):
"""
Create a csv with the format required by aicrowd
----------
ids : list
List of pairs of (row,column) indices
y_pred : list
List of prediction of the rating
name : String
The name of the csv file to be created
"""
with open(name, 'w') as csvfile:
fieldnames = ['Id', 'Prediction']
writer = csv.DictWriter(csvfile, delimiter=",", fieldnames=fieldnames)
writer.writeheader()
if(1176952!=len(list(zip(ids, y_pred)))):
print("Error! Missmatch in the lengs of the csv rows")
for r1, r2 in zip(ids, y_pred):
writer.writerow({'Id': r1,'Prediction': r2})
def preprocess(data):
"""
Convert a dataframe of indices of the given aicrowd input format to simple row/column dataframe
----------
data : Dataframe
The input in aicrowd format
Returns:
A dataframe of user/item indices
"""
df = data.copy()
# process removing the r or c and converting them into integers
# Remove 1 from the received indexes as to have the matrices start at index 0
rs = [int(r[0][1:])-1 for r in df.index.str.split('_')] #Take the r part and remove the letter
cs = [int(c[1][1:])-1 for c in df.index.str.split('_')] #Take the c part and remove the letter
df['user'], df['item'] = rs, cs
return df
def read_file():
"""
Read the sample_submission.csv file and extract the information into 3 lists
----------
Returns:
A list containing the lines of the csv file, a list containing the rwo index and a list containing the column index
"""
data=read_txt("data/sampleSubmission.csv")
test_indices=predict_on_model_line(data[1:])
uid=[]
iid=[]
ids = []
for i,j in test_indices:
ids.append("r{0}_c{1}".format(i+1,j+1))
uid.append(i)
iid.append(j)
return ids,uid,iid
def pred_from_suprise_to_df(model_pred):
"""
Create a Dataframe starting from a Surprise data set containing only the prediction
----------
Returns:
The Dataframe containing the prediction extracted from the input
"""
list=[]
for pred in model_pred:
list.append(pred.est)
return pd.DataFrame(list)
def trainset_from_surprise_to_df(trainset):
"""
Create a Dataframe starting from a Surprise data set
----------
Returns:
The Dataframe containing the 'User', 'Movie', 'Rating' columns extracted from the input
"""
df_trainset=[]
for u,m,r in trainset.all_ratings():
df_trainset.append([u,m,r])
df_trainset = pd.DataFrame(df_trainset)
df_trainset = df_trainset.rename({0:'User',1:'Movie',2:'Rating'},axis =1)
return df_trainset
def predict_on_model(algo):
"""
Use the provided model to predict on the user/movie indices present on the sample_submission.csv. The predictions are rounded up to the closest integer
----------
algo: surprise.prediction_algorithms
The model to be used in the prediction
Returns:
A list of (row/column) pairs and a list of the prediction in those indices
"""
data=read_txt("data/sampleSubmission.csv")
test_indices=predict_on_model_line(data[1:])
preds=[]
ids=[]
for i,j in test_indices:
ids.append("r{0}_c{1}".format(i+1,j+1))
uid= i
iid= j
pred = algo.predict(uid,iid)
preds.append(int(round(pred.est)))
return ids, preds
def predict_on_models(surprise_models, mf_sgd_pair, mf_als_pair, bl_global,df_featured,global_average,surprise_weights,models_weights):
"""
Use the provided models to predict on the user/movie indices present on the sample_submission.csv and combine the result with the provided weights. The predictions are rounded up to the closest integer
----------
models: list
List of models to be used in the prediction
weights: list
List of weights corresponding to each of the provided models
Returns:
A list of (row/column) pairs and a list of the prediction in those indices
"""
zippedMW=list(zip(surprise_models,surprise_weights))
data=read_txt("data/sampleSubmission.csv")
test_indices=predict_on_model_line(data[1:])
preds=[]
ids=[]
for i,j in test_indices:
ids.append("r{0}_c{1}".format(i+1,j+1))
uid= i
iid= j
pred=0
for m, w in zippedMW:
pred = pred+m.predict(uid,iid).est*w
#MFSGD
user_sgd, movie_sgd = mf_sgd_pair
user_data_sgd = user_sgd[:,i]
movie_data_sgd = movie_sgd[:,j]
prediciton_sgd= movie_data_sgd @ user_data_sgd.T
pred = pred + prediciton_sgd* models_weights['MFSGD']
#MFALS
user_als, movie_als = mf_sgd_pair
user_data_als = user_als[:,i]
movie_data_als = movie_als[:,j]
prediciton_als= movie_data_als @ user_data_als.T
pred = pred + prediciton_als* models_weights['MFALS']
#baseline Global
pred = pred + bl_global* models_weights['BLGlobal']
#Features
udf=df_featured[(df_featured.User == uid)].drop(['User','Movie'],axis = 1 )
if(len(udf)>0):
userAvg = udf.values[0][0]
else:
userAvg=global_average
mdf=df_featured[(df_featured.Movie == iid)].drop(['User','Movie'],axis = 1 )
if(len(mdf)>0):
movieAvg = mdf.values[0][1]
else:
movieAvg=global_average
pred = pred +userAvg* models_weights['User_Average']
pred = pred +movieAvg* models_weights['Movie_Average']
pred = int(round(pred))
pred = max(pred,1)
pred = min(pred,5)
preds.append(pred)
return ids, preds
def predict_on_models_logistic(models, weights):
"""
Use the provided models to predict on the user/movie indices present on the sample_submission.csv and combine the result with the provided weights. The predictions are rounded up to the closest integer
----------
models: list
List of models to be used in the prediction
weights: list
List of weights corresponding to each of the provided models
Returns:
A list of (row/column) pairs and a list of the prediction in those indices
"""
data=read_txt("data/sampleSubmission.csv")
test_indices=predict_on_model_line(data[1:])
preds=[]
ids=[]
for i,j in test_indices:
ids.append("r{0}_c{1}".format(i+1,j+1))
uid= i
iid= j
pred_list=[]
for wclass in weights:
zippedMW=list(zip(models,wclass))
pred = 0
for m,w in zippedMW:
# print("m:",m)
# print("w:",w)
pred = pred+m.predict(uid,iid).est*w
pred = np.exp(pred) / (1 + np.exp(pred) )
pred_list.append(pred)
# print(pred_list)
preds.append(np.argmax(np.array(pred_list))+1)
return ids, preds
def predict_on_models_xgb(models, df_features , xgb_model):
"""
Use the provided models and augmented features to predict on the user/movie indices present on the sample_submission.csv and combine the result with the provided weights. The predictions are rounded up to the closest integer
----------
models: list
List of models to be used in the prediction
df_features: Dataframe
Dataframe containing additional features on the data
xgb_model: xgboost
A xgboost ensemble model trained on the provided prediction models
Returns:
A list of (row/column) pairs and a list of the prediction in those indices
"""
data=read_txt("data/sampleSubmission.csv")
test_indices=predict_on_model_line(data[1:])
preds=[]
ids=[]
for i,j in test_indices:
ids.append("r{0}_c{1}".format(i+1,j+1))
uid= i
iid= j
model_preds=[]
for m in models:
model_preds.append(m.predict(uid,iid).est)
df_models = pd.DataFrame(np.reshape(model_preds, (1,-1)), columns = ['dfCC', 'dfBL', 'dfSVD', 'dfSVDpp', 'dfNMF', 'dfKNNMovie', 'dfKNNUser','dfSO'] )
features_row = df_features[(df_features.User == uid) & (df_features.Movie == iid)].drop(['User','Movie'],axis = 1 )
df_merged=pd.concat([df_models, features_row], axis=1)
res=xgb_model.predict(df_merged)
preds.append(res)
return ids, preds
def predict_on_all_models_and_features_xgb(xgb_model,models, mf_sgd_pair, mf_als_pair, bl_global, bl_movie,bl_user, df_features):
"""
Use the provided models and augmented features to predict on the user/movie indices present on the sample_submission.csv and combine the result with the provided weights.
The predictions are computed in different ways depending on the model/feature.
The predictions are rounded up to the closest integer
----------
xgb_model: xgboost
A xgboost ensemble model trained on the provided prediction models
models: list
List of Surprise models to be used in the prediction
mf_sgd_pair: list
A list of 2 elements containing the resulting matrices of Matrix factorization using SGD
mf_sals_pair: list
A list of 2 elements containing the resulting matrices of Matrix factorization using ALS
bl_global: float
The baseline global mean
bl_movie: list
The baseline Movie mean
bl_user: list
The baseline User mean
df_features: Dataframe
Dataframe containing additional features on the data
Returns:
A list of (row/column) pairs and a list of the prediction in those indices
"""
data=read_txt("data/sampleSubmission.csv")
test_indices=predict_on_model_line(data[1:])
preds=[]
ids=[]
for i,j in test_indices:
ids.append("r{0}_c{1}".format(i+1,j+1))
uid= i
iid= j
model_preds=[]
for m in models:
model_preds.append(m.predict(uid,iid).est)
#MFSGD
user_sgd, movie_sgd = mf_sgd_pair
user_data_sgd = user_sgd[:,i]
movie_data_sgd = movie_sgd[:,j]
prediciton_sgd= movie_data_sgd @ user_data_sgd.T
model_preds.append(prediciton_sgd)
#MFALS
user_als, movie_als = mf_sgd_pair
user_data_als = user_als[:,i]
movie_data_als = movie_als[:,j]
prediciton_als= movie_data_als @ user_data_als.T
model_preds.append(prediciton_als)
#baseline Global
model_preds.append(bl_global)
#Baseline Movie
model_preds.append(bl_movie[j,0])
#Baseline User
model_preds.append(bl_user[0,i])
#Concatenate the results on models
df_models = pd.DataFrame(np.reshape(model_preds, (1,-1)), columns = ['dfCC','dfBL','dfSVD','dfSVDpp','dfNMF','dfKNNMovie','dfKNNUser','dfSO','dfMFSGD','dfMFALS','dfBLGlobal','dfBLMovie','dfBLUser'] )
#Predict on augmented features
features_row = df_features[(df_features.User == uid) & (df_features.Movie == iid)].drop(['User','Movie'],axis = 1 )
df_merged=pd.concat([df_models, features_row], axis=1)
res=xgb_model.predict(df_merged)
preds.append(max(1,min(5,int(round(res[0])))))
return ids, preds