-
Notifications
You must be signed in to change notification settings - Fork 6
/
matrix_factorization.py
834 lines (648 loc) · 32.8 KB
/
matrix_factorization.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# import library files
from Test import Test
from Initializer import initializer
from Similarities import Similarities
# import other libraries
import numpy as np
import copy
import bisect
class SVDpp():
def __init__(self):
self.set_hyperparameters()
def set_hyperparameters(self, initialization_method='random', init_mean=0, init_std=1, biased=False, n_latent=20, n_epochs=20,
lr_all=.007, reg_all=.02, lr_bu=None, lr_bi=None, lr_pu=None,
lr_qi=None, lr_yj=None, reg_bu=None, reg_bi=None, reg_pu=None,
reg_qi=None, reg_yj=None, random_state=None, verbose=False):
self.n_latent = n_latent
self.n_epochs = n_epochs
self.biased = biased
self.initialization_method = initialization_method
self.init_mean = init_mean
self.init_std = init_std
self.lr_bu = lr_bu if lr_bu is not None else lr_all
self.lr_bi = lr_bi if lr_bi is not None else lr_all
self.lr_pu = lr_pu if lr_pu is not None else lr_all
self.lr_qi = lr_qi if lr_qi is not None else lr_all
self.lr_yj = lr_yj if lr_yj is not None else lr_all
self.reg_bu = reg_bu if reg_bu is not None else reg_all
self.reg_bi = reg_bi if reg_bi is not None else reg_all
self.reg_pu = reg_pu if reg_pu is not None else reg_all
self.reg_qi = reg_qi if reg_qi is not None else reg_all
self.reg_yj = reg_yj if reg_yj is not None else reg_all
self.random_state = random_state
self.verbose = verbose
def __set_data(self, data, test_portion):
# get distinct users, items and user_existing_ratings, items_existing_users
self.user_existing_ratings = {}
self.items_rated_by_users = {}
self.user_ids = []
self.item_ids = []
self.all_ratings_in_train = 0
np.random.shuffle(data)
# variables for train and test split
user_dictionary = {}
item_dictionary = {}
self.user_n_ratings = {}
self.item_n_ratings = {}
self.train_data = []
self.test_data = []
self.train_data_user_ids = []
self.train_data_item_ids = []
self.test_data_user_ids = []
self.test_data_item_ids = []
for user, item, score in data:
# Unique users and items
try:
user = int(user)
except:
pass
try:
item = int(item)
except:
pass
user = str(user)
item = str(item)
score = float(score)
if user not in self.user_existing_ratings:
self.user_ids.append(user)
if item not in self.items_rated_by_users:
self.item_ids.append(item)
self.items_rated_by_users.setdefault(item, []).append(user)
self.user_existing_ratings.setdefault(user, []).append(item)
ratio = len(self.test_data) / (len(self.train_data)+0.001)
if self.test_split:
# train and test set
user_dictionary.setdefault(user, 0)
item_dictionary.setdefault(item, 0)
if user_dictionary[user] * test_portion >= 1 and item_dictionary[item] * test_portion >= 1 and ratio <= test_portion+0.02:
self.test_data.append([user, item, score])
if user not in self.test_data_user_ids: self.test_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.test_data_item_ids.append(item)
try: self.user_n_ratings[user] += 1
except KeyError: self.user_n_ratings.setdefault(user, 1)
try: self.item_n_ratings[item] += 1
except KeyError: self.item_n_ratings.setdefault(item, 1)
user_dictionary[user] -= 1
item_dictionary[item] -= 1
else:
self.train_data.append([user, item, score])
self.all_ratings_in_train += score
if user not in self.train_data_user_ids: self.train_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.train_data_item_ids.append(item)
try: self.user_n_ratings[user] += 1
except KeyError: self.user_n_ratings.setdefault(user, 1)
try: self.item_n_ratings[item] += 1
except KeyError: self.item_n_ratings.setdefault(item, 1)
user_dictionary[user] += 1
item_dictionary[item] += 1
else:
self.train_data.append([user, item, score])
self.all_ratings_in_train += score
if user not in self.train_data_user_ids: self.train_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.train_data_item_ids.append(item)
try: self.user_n_ratings[user] += 1
except KeyError: self.user_n_ratings.setdefault(user, 1)
try: self.item_n_ratings[item] += 1
except KeyError: self.item_n_ratings.setdefault(item, 1)
print('Your data has {} distinct users and {} distinct items.'.format(
len(self.user_ids), len(self.item_ids)))
if len(self.test_data) < 1 and self.test_split:
self.test_split = False
self.early_stopping = False
print("Training set doesn't have enough data for given test portion.")
if self.test_split:
print('Your data has been split into train and test set.')
print('Length of training set is {}. Length of Test set is {}'.format(
len(self.train_data), len(self.test_data)))
else:
print('Your data has no test set.')
print('Length of training set is {}'.format(len(self.train_data)))
def fit(self, data,test_split=True, test_portion=0.1, search_parameter_space=False):
lr_bu = self.lr_bu
lr_bi = self.lr_bi
lr_pu = self.lr_pu
lr_qi = self.lr_qi
lr_yj = self.lr_yj
reg_bu = self.reg_bu
reg_bi = self.reg_bi
reg_pu = self.reg_pu
reg_qi = self.reg_qi
reg_yj = self.reg_yj
if not search_parameter_space:
self.test_split = test_split
self.__set_data(data, test_portion)
print('Initializing features for Users and Items...')
initial = initializer(self.user_ids, self.item_ids, self.initialization_method,
self.n_latent, self.init_mean, self.init_std)
self.pu, self.qi = initial.initialize_latent_vectors(initalization_method='normal')
_, self.yj = initial.initialize_latent_vectors(initalization_method='normal')
self.bu = dict([(key, 0) for key in self.train_data_user_ids])
self.bi = dict([(key, 0) for key in self.train_data_item_ids])
if not self.biased:
global_mean = 0
else:
global_mean = self.all_ratings_in_train / len(self.train_data)
for current_epoch in range(self.n_epochs):
if self.verbose:
print(" processing epoch {}".format(current_epoch))
for u, i, r in self.train_data:
# items rated by u
self.Iu = [items for items in self.user_existing_ratings[u]]
self.sqrt_Iu = np.sqrt(len(self.Iu))
# implicit feedback
self.u_impl_fdb = np.zeros(self.n_latent, np.double)
for j in self.Iu:
for f in range(self.n_latent):
self.u_impl_fdb[f] += self.yj[j][f] / self.sqrt_Iu
# compute current error
dot = 0
for f in range(self.n_latent):
dot += self.qi[i][f] * (self.pu[u][f] + self.u_impl_fdb[f])
err = r - (global_mean + self.bu[u] + self.bi[i] + dot)
# update biases
self.bu[u] += lr_bu * (err - reg_bu * self.bu[u])
self.bi[i] += lr_bi * (err - reg_bi * self.bi[i])
# update factors
for f in range(self.n_latent):
puf = self.pu[u][f]
qif = self.qi[i][f]
self.pu[u][f] += lr_pu * (err * qif - reg_pu * puf)
self.qi[i][f] += lr_qi * (err * puf - reg_qi * qif)
for j in self.Iu:
self.yj[j][f] += lr_yj * (err * qif / self.sqrt_Iu -
reg_yj * self.yj[j][f])
# Calculate errors
#error_counter += 1
train_error = Test.rmse_error(
self.train_data, self.pu, self.qi)
# Show error to Client
if self.test_split:
test_error = Test.rmse_error(
self.test_data, self.pu, self.qi)
print('Epoch Number: {}/{} Training RMSE: {:.2f} Test RMSE: {}'.format(current_epoch+1, self.n_epochs,
train_error, test_error))
else:
print('Epoch Number: {}/{} Training RMSE: {:.2f}'.format(current_epoch+1, self.n_epochs,
train_error))
self.bu = self.bu
self.bi = self.bi
self.pu = self.pu
self.qi = self.qi
self.yj = self.yj
def get_recommendation_for_existing_user(self, user_id, howMany=10):
result_list = []
# this might be more effective using matrix multiplication
for item in self.item_ids:
# if user did not already rate the item
if item not in self.user_existing_ratings[user_id]:
prediction = np.dot(
self.pu[user_id], self.qi[item])
bisect.insort(result_list, [prediction, item])
return [x[1] for x in result_list[::-1][0:howMany]]
def get_recommendation_for_new_user(self, user_ratings,
similarity_measure='mean_squared_difference', howManyUsers=3, howManyItems=5):
# Get user predictions on same movies
user_predictions = self.__user_prediction_for_same_movies(user_ratings)
# Find most most similar user_ids
user_ids = Similarities.get_most_similar_users(
user_ratings, user_predictions, similarity_measure, howManyUsers)
result_list = []
# get user features for users who are most similar to given new user
for user in user_ids:
for item, item_feature in self.qi.items():
# predict ratings for most similar users
prediction = np.dot(
self.pu[user], item_feature)
bisect.insort(result_list, [prediction, item])
# remove duplicates
return_list = []
for pair in result_list:
if len(return_list) >= howManyItems:
break
if pair[1] in return_list:
continue
return_list.append(pair[1])
return return_list
def __user_prediction_for_same_movies(self, user_ratings):
result = {}
for key in user_ratings:
if key not in self.qi:
continue
for user in self.pu:
result.setdefault(user, []).append(
np.dot(self.pu[user], self.qi[key]))
return result
class NMF():
def __init__(self):
self.set_hyperparameters()
def set_hyperparameters(self, initialization_method='random',n_latent=15, n_epochs=30, biased=False, reg_user_features=.06,
reg_item_features=.06, reg_bu=.02, reg_bi=.02, lr_bu=.005, lr_bi=.005,random_state=None, verbose=False):
self.n_latent = n_latent
self.initialization_method = initialization_method
self.n_epochs = n_epochs
self.biased = biased
self.reg_user_features = reg_user_features
self.reg_item_features = reg_item_features
self.lr_bu = lr_bu
self.lr_bi = lr_bi
self.reg_bu = reg_bu
self.reg_bi = reg_bi
self.random_state = random_state
self.verbose = verbose
def __set_data(self, data, test_portion):
# get distinct users, items and user_existing_ratings, items_existing_users
self.user_existing_ratings = {}
self.items_rated_by_users = {}
self.user_ids = []
self.item_ids = []
self.all_ratings_in_train = 0
np.random.shuffle(data)
# variables for train and test split
user_dictionary = {}
item_dictionary = {}
self.user_n_ratings = {}
self.item_n_ratings = {}
self.train_data = []
self.test_data = []
self.train_data_user_ids = []
self.train_data_item_ids = []
self.test_data_user_ids = []
self.test_data_item_ids = []
for user, item, score in data:
# Unique users and items
try:
user = int(user)
except:
pass
try:
item = int(item)
except:
pass
user = str(user)
item = str(item)
score = float(score)
if user not in self.user_existing_ratings:
self.user_ids.append(user)
if item not in self.items_rated_by_users:
self.item_ids.append(item)
self.items_rated_by_users.setdefault(item, []).append(user)
self.user_existing_ratings.setdefault(user, []).append(item)
ratio = len(self.test_data) / (len(self.train_data)+0.001)
if self.test_split:
# train and test set
user_dictionary.setdefault(user, 0)
item_dictionary.setdefault(item, 0)
if user_dictionary[user] * test_portion >= 1 and item_dictionary[item] * test_portion >= 1 and ratio <= test_portion+0.02:
self.test_data.append([user, item, score])
if user not in self.test_data_user_ids: self.test_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.test_data_item_ids.append(item)
try: self.user_n_ratings[user] += 1
except KeyError: self.user_n_ratings.setdefault(user, 1)
try: self.item_n_ratings[item] += 1
except KeyError: self.item_n_ratings.setdefault(item, 1)
user_dictionary[user] -= 1
item_dictionary[item] -= 1
else:
self.train_data.append([user, item, score])
self.all_ratings_in_train += score
if user not in self.train_data_user_ids: self.train_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.train_data_item_ids.append(item)
try: self.user_n_ratings[user] += 1
except KeyError: self.user_n_ratings.setdefault(user, 1)
try: self.item_n_ratings[item] += 1
except KeyError: self.item_n_ratings.setdefault(item, 1)
user_dictionary[user] += 1
item_dictionary[item] += 1
else:
self.train_data.append([user, item, score])
self.all_ratings_in_train += score
if user not in self.train_data_user_ids: self.train_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.train_data_item_ids.append(item)
try: self.user_n_ratings[user] += 1
except KeyError: self.user_n_ratings.setdefault(user, 1)
try: self.item_n_ratings[item] += 1
except KeyError: self.item_n_ratings.setdefault(item, 1)
print('Your data has {} distinct users and {} distinct items.'.format(
len(self.user_ids), len(self.item_ids)))
if len(self.test_data) < 1 and self.test_split:
self.test_split = False
self.early_stopping = False
print("Training set doesn't have enough data for given test portion.")
if self.test_split:
print('Your data has been split into train and test set.')
print('Length of training set is {}. Length of Test set is {}'.format(
len(self.train_data), len(self.test_data)))
else:
print('Your data has no test set.')
print('Length of training set is {}'.format(len(self.train_data)))
def fit(self, data,test_split=True, test_portion=0.1, search_parameter_space=False):
if not search_parameter_space:
self.test_split = test_split
self.__set_data(data, test_portion)
print('Initializing features for Users and Items...')
initial = initializer(self.user_ids, self.item_ids, self.initialization_method,
self.n_latent, 0,0)
user_features, item_features = initial.initialize_latent_vectors()
bu = dict([(key, 0) for key in self.train_data_user_ids])
bi = dict([(key, 0) for key in self.train_data_item_ids])
if not self.biased:
global_mean = 0
else:
global_mean = self.all_ratings_in_train / len(self.train_data)
for current_epoch in range(self.n_epochs):
if self.verbose:
print("Processing epoch {}".format(current_epoch))
# (re)initialize nums and denoms to zero
self.user_num = dict([(key, np.zeros(self.n_latent)) for key in self.train_data_user_ids])
self.user_denom = dict([(key, np.zeros(self.n_latent)) for key in self.train_data_user_ids])
self.item_num = dict([(key, np.zeros(self.n_latent)) for key in self.train_data_item_ids])
self.item_denom = dict([(key, np.zeros(self.n_latent)) for key in self.train_data_item_ids])
for u, i, r in self.train_data:
# compute current estimation and error
dot = 0 # <q_i, p_u>
for f in range(self.n_latent):
dot += user_features[u][f] * item_features[i][f]
est = global_mean + bu[u] + bi[i] + dot
err = r - est
# Update biases
if self.biased:
bu[u] += self.lr_bu * (err - self.reg_bu * bu[u])
bi[i] += self.lr_bi * (err - self.reg_bi * bi[i])
# Compute numerators and denominators
for f in range(self.n_latent):
self.user_num[u][f] += item_features[i][f] * r
self.user_denom[u][f] += item_features[i][f] * est
self.item_num[i][f] += user_features[u][f] * r
self.item_denom[i][f] += user_features[u][f] * est
# Update user factors
for u in self.train_data_user_ids:
n_ratings = self.user_n_ratings[u]
for f in range(self.n_latent):
self.user_denom[u][f] += n_ratings * self.reg_user_features * user_features[u][f]
user_features[u][f] *= self.user_num[u][f] / self.user_denom[u][f]
# Update item factors
for i in self.train_data_item_ids:
n_ratings = self.item_n_ratings[i]
for f in range(self.n_latent):
self.item_denom[i][f] += n_ratings * self.reg_item_features * item_features[i][f]
item_features[i][f] *= self.item_num[i][f] / self.item_denom[i][f]
# Calculate errors
#error_counter += 1
train_error = Test.rmse_error(
self.train_data, user_features, item_features)
# Show error to Client
if self.test_split:
test_error = Test.rmse_error(
self.test_data, user_features, item_features)
print('Epoch Number: {}/{} Training RMSE: {:.2f} Test RMSE: {}'.format(current_epoch+1, self.n_epochs,
train_error, test_error))
else:
print('Epoch Number: {}/{} Training RMSE: {:.2f}'.format(current_epoch+1, self.n_epochs,
train_error))
self.bu = bu
self.bi = bi
self.user_features = user_features
self.item_features = item_features
def get_recommendation_for_existing_user(self, user_id, howMany=10):
result_list = []
# this might be more effective using matrix multiplication
for item in self.item_ids:
# if user did not already rate the item
if item not in self.user_existing_ratings[user_id]:
prediction = np.dot(
self.user_features[user_id], self.item_features[item])
bisect.insort(result_list, [prediction, item])
return [x[1] for x in result_list[::-1][0:howMany]]
def get_recommendation_for_new_user(self, user_ratings,
similarity_measure='mean_squared_difference', howManyUsers=3, howManyItems=5):
# Get user predictions on same movies
user_predictions = self.__user_prediction_for_same_movies(user_ratings)
# Find most most similar user_ids
user_ids = Similarities.get_most_similar_users(
user_ratings, user_predictions, similarity_measure, howManyUsers)
result_list = []
# get user features for users who are most similar to given new user
for user in user_ids:
for item, item_feature in self.item_features.items():
# predict ratings for most similar users
prediction = np.dot(
self.user_features[user], item_feature)
bisect.insort(result_list, [prediction, item])
# remove duplicates
return_list = []
for pair in result_list:
if len(return_list) >= howManyItems:
break
if pair[1] in return_list:
continue
return_list.append(pair[1])
return return_list
def __user_prediction_for_same_movies(self, user_ratings):
result = {}
for key in user_ratings:
if key not in self.item_features:
continue
for user in self.user_features:
result.setdefault(user, []).append(
np.dot(self.user_features[user], self.item_features[key]))
return result
class FunkSVD():
def __init__(self):
# Initialize default hyperparameters
self.set_hyperparameters()
def set_hyperparameters(self, initialization_method='random', max_epoch=5, n_latent=10, learning_rate=0.01, regularization=0.1, early_stopping=False, init_mean=0, init_std=1):
self.initialization_method = initialization_method
self.max_epoch = max_epoch
self.n_latent = n_latent
self.learning_rate = learning_rate
self.regularization = regularization
self.early_stopping = early_stopping
self.init_mean = init_mean
self.init_std = init_std
self.min_train_error = np.inf
self.min_test_error = np.inf
def __set_data(self, data, test_portion):
# get distinct users, items and user_existing_ratings, items_existing_users
self.user_existing_ratings = {}
self.items_rated_by_users = {}
self.user_ids = []
self.item_ids = []
np.random.shuffle(data)
# variables for train and test split
user_dictionary = {}
item_dictionary = {}
self.train_data = []
self.test_data = []
self.train_data_user_ids = []
self.train_data_item_ids = []
self.test_data_user_ids = []
self.test_data_item_ids = []
for user, item, score in data:
# Unique users and items
try:
user = int(user)
except:
pass
try:
item = int(item)
except:
pass
user = str(user)
item = str(item)
score = float(score)
if user not in self.user_existing_ratings:
self.user_ids.append(user)
if item not in self.items_rated_by_users:
self.item_ids.append(item)
self.items_rated_by_users.setdefault(item, []).append(user)
self.user_existing_ratings.setdefault(user, []).append(item)
ratio = len(self.test_data) / (len(self.train_data)+0.001)
if self.test_split:
# train and test set
user_dictionary.setdefault(user, 0)
item_dictionary.setdefault(item, 0)
if user_dictionary[user] * test_portion >= 1 and item_dictionary[item] * test_portion >= 1 and ratio <= test_portion+0.02:
self.test_data.append([user, item, score])
if user not in self.test_data_user_ids: self.test_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.test_data_item_ids.append(item)
user_dictionary[user] -= 1
item_dictionary[item] -= 1
else:
self.train_data.append([user, item, score])
if user not in self.train_data_user_ids: self.train_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.train_data_item_ids.append(item)
user_dictionary[user] += 1
item_dictionary[item] += 1
else:
self.train_data.append([user, item, score])
if user not in self.train_data_user_ids: self.train_data_user_ids.append(user)
if item not in self.train_data_item_ids: self.train_data_item_ids.append(item)
print('Your data has {} distinct users and {} distinct items.'.format(
len(self.user_ids), len(self.item_ids)))
if len(self.test_data) < 1 and self.test_split:
self.test_split = False
self.early_stopping = False
print("Training set doesn't have enough data for given test portion.")
if self.test_split:
print('Your data has been split into train and test set.')
print('Length of training set is {}. Length of Test set is {}'.format(
len(self.train_data), len(self.test_data)))
else:
print('Your data has no test set.')
print('Length of training set is {}'.format(len(self.train_data)))
def fit(self, data, test_split=True, test_portion=0.1, search_parameter_space=False):
# Set train_data, test_data, user_ids etc. if search parameter is False
# If True, this lets us search parameter space with the same train-test split
if not search_parameter_space:
self.test_split = test_split
self.__set_data(data, test_portion)
# Initialization
print('Initializing features for Users and Items...')
initial = initializer(self.user_ids, self.item_ids, self.initialization_method,
self.n_latent, self.init_mean, self.init_std)
self.user_features, self.item_features = initial.initialize_latent_vectors()
# Training
print('Starting training...')
error_counter = 0
for epoch in range(self.max_epoch):
# updating user and item features
for user, item, rating in self.train_data:
error = rating - \
np.dot(self.user_features[user], self.item_features[item])
# Use temp to update each item and user feature in sync.
temp = self.user_features[user]
# Update user and item feature for each user, item and rating pair
self.user_features[user] += self.learning_rate * \
(error * self.item_features[item] -
self.regularization * self.user_features[user])
self.item_features[item] += self.learning_rate * \
(error * temp - self.regularization *
self.item_features[item])
# Calculate errors
error_counter += 1
train_error = Test.rmse_error(
self.train_data, self.user_features, self.item_features)
# Show error to Client
if self.test_split:
test_error = Test.rmse_error(
self.test_data, self.user_features, self.item_features)
print('Epoch Number: {}/{} Training RMSE: {:.2f} Test RMSE: {}'.format(epoch+1, self.max_epoch,
train_error, test_error))
else:
print('Epoch Number: {}/{} Training RMSE: {:.2f}'.format(epoch+1, self.max_epoch,
train_error))
# Save best features depending on test_error
if self.test_split and test_error < self.min_test_error:
self.min_test_error = test_error
self.best_user_features = copy.deepcopy(self.user_features)
self.best_item_features = copy.deepcopy(self.item_features)
error_counter = 0
# Save best features if test data is False
elif not self.test_split and train_error < self.min_train_error:
self.min_train_error = train_error
self.best_user_features = copy.deepcopy(self.user_features)
self.best_item_features = copy.deepcopy(self.item_features)
# Break if test_error didn't improve for the last n rounds and early stopping is true
if self.early_stopping and error_counter >= self.early_stopping:
print("Test error didn't get lower for the last {} epochs. Training is stopped.".format(
error_counter))
print('Best test error is: {:.2f}. Best features are saved.'.format(
self.min_test_error))
break
print('Training has ended...')
self.user_features = copy.deepcopy(self.best_user_features)
self.item_features = copy.deepcopy(self.best_item_features)
def get_recommendation_for_existing_user(self, user_id, howMany=10):
result_list = []
# this might be more effective using matrix multiplication
for item in self.item_ids:
# if user did not already rate the item
if item not in self.user_existing_ratings[user_id]:
prediction = np.dot(
self.user_features[user_id], self.item_features[item])
bisect.insort(result_list, [prediction, item])
return [x[1] for x in result_list[::-1][0:howMany]]
def get_recommendation_for_new_user(self, user_ratings,
similarity_measure='mean_squared_difference', howManyUsers=3, howManyItems=5):
# Get user predictions on same movies
user_predictions = self.__user_prediction_for_same_movies(user_ratings)
# Find most most similar user_ids
user_ids = Similarities.get_most_similar_users(
user_ratings, user_predictions, similarity_measure, howManyUsers)
result_list = []
# get user features for users who are most similar to given new user
for user in user_ids:
for item, item_feature in self.item_features.items():
# predict ratings for most similar users
prediction = np.dot(
self.user_features[user], item_feature)
bisect.insort(result_list, [prediction, item])
# remove duplicates
return_list = []
for pair in result_list:
if len(return_list) >= howManyItems:
break
if pair[1] in return_list:
continue
return_list.append(pair[1])
return return_list
def get_similar_products(self, item_id, howMany=10):
result_list = []
product_features = self.item_features[item_id]
for item in self.item_ids:
if item == item_id:
continue
# add cosine sim function from similarites
cos_sim = Similarities.cosine_similarity(
self.item_features[item], product_features)
bisect.insort(result_list, [cos_sim, item])
return [x[1] for x in result_list[::-1][0:howMany]]
def __user_prediction_for_same_movies(self, user_ratings):
result = {}
for key in user_ratings:
if key not in self.item_features:
continue
for user in self.user_features:
result.setdefault(user, []).append(
np.dot(self.user_features[user], self.item_features[key]))
return result