-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpreprocessing.py
More file actions
executable file
·1168 lines (993 loc) · 44.2 KB
/
preprocessing.py
File metadata and controls
executable file
·1168 lines (993 loc) · 44.2 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
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
from __future__ import print_function
import sys
import numpy as np
import scipy.sparse as sp
import pickle as pkl
import os
import h5py
import pandas as pd
import pdb
import random
from scipy.sparse import linalg
from data_utils import load_data, map_data, download_dataset
from sklearn.metrics import mean_squared_error
from math import sqrt
def normalize_features(feat):
degree = np.asarray(feat.sum(1)).flatten()
# set zeros to inf to avoid dividing by zero
degree[degree == 0.] = np.inf
degree_inv = 1. / degree
degree_inv_mat = sp.diags([degree_inv], [0])
feat_norm = degree_inv_mat.dot(feat)
if feat_norm.nnz == 0:
print('ERROR: normalized adjacency matrix has only zero entries!!!!!')
exit
return feat_norm
def load_own_file(path_file, filter_num = 10):
user_dic = {}
item_dic = {}
user_item_dic = {}
user_id = 0
item_id = 0
user_select_id = 0
item_select_id = 0
user_select = {}
item_select = {}
index = []
indptr = []
data = []
for line in open(path_file):
groups = line.strip().split("\t")
if groups[3] == "NULL" or groups[3] == '':
continue
if groups[0] not in user_dic:
user_dic[groups[0]] = [user_id, 1]
user_id += 1
else:
user_dic[groups[0]][1] += 1
if groups[1] not in item_dic:
item_dic[groups[1]] = [item_id, 1]
item_id += 1
else:
item_dic[groups[1]][1] += 1
if groups[1] not in item_select and item_dic[groups[1]][1] > filter_num:
item_select[groups[1]] = item_select_id
item_select_id += 1
index.append(groups[0])
indptr.append(groups[1])
data.append(float(groups[3]))
for i in range(len(index)):
user_id = index[i]
item_id = indptr[i]
if item_id in item_select and user_id not in user_select:
user_select[user_id] = user_select_id
user_select_id += 1
Train_index = []
Train_indptr = []
Train_data = []
Test_index = []
Test_indptr = []
Test_data = []
All_index = []
All_indptr = []
All_data = []
#filter
for i in range(len(index)):
user_id = index[i]
item_id = indptr[i]
if user_id in user_select and item_id in item_select:
All_index.append(user_select[user_id])
All_indptr.append(item_select[item_id])
All_data.append(data[i])
if np.random.randint(0,100) < 20:
Train_index.append(user_select[user_id])
Train_indptr.append(item_select[item_id])
Train_data.append(1)
else:
Test_index.append(user_select[user_id])
Test_indptr.append(item_select[item_id])
Test_data.append(1)
M = sp.csr_matrix((All_data, (All_index, All_indptr)), shape=(len(user_select), len(item_select)))
O_train = sp.csr_matrix((Train_data, (Train_index, Train_indptr)), shape=(len(user_select), len(item_select)))
O_test = sp.csr_matrix((Test_data, (Test_index, Test_indptr)), shape=(len(user_select), len(item_select)))
return None, M.toarray(), O_train.toarray(), O_test.toarray(), user_select, item_select
def load_own_file2(path_file, filter_num = 0):
user_dic = {}
item_dic = {}
user_item_dic = {}
user_id = 0
item_id = 0
user_select_id = 0
item_select_id = 0
user_select = {}
item_select = {}
index = []
indptr = []
data = []
data_c = 0
data_dic = {}
most = 0
for line in open(path_file):
groups = line.strip().split("\t")
if groups[3] == "NULL" or groups[3] == '':
continue
if groups[0] not in user_dic:
user_dic[groups[0]] = [user_id, 1]
user_id += 1
else:
user_dic[groups[0]][1] += 1
if groups[1] not in item_dic:
item_dic[groups[1]] = [item_id, 1]
item_id += 1
else:
item_dic[groups[1]][1] += 1
if item_dic[groups[1]][1] > most:
most = item_dic[groups[1]][1]
if groups[1] not in item_select and item_dic[groups[1]][1] > filter_num:
item_select[groups[1]] = item_select_id
item_select_id += 1
index.append(groups[0])
indptr.append(groups[1])
if groups[0] + "\t" + groups[1] in data_dic:
last_id = data_dic[groups[0] + "\t" + groups[1]]
data[last_id] = -1.0
data_dic[groups[0] + "\t" + groups[1]] = data_c
data.append(float(groups[3]) - 1)
data_c += 1
print("most:", most)
for i in range(len(index)):
user_id = index[i]
item_id = indptr[i]
if item_id in item_select and user_id not in user_select:
user_select[user_id] = user_select_id
user_select_id += 1
Train_index = []
Train_indptr = []
Train_data = []
Val_index = []
Val_indptr = []
Val_data = []
Test_index = []
Test_indptr = []
Test_data = []
All_index = []
All_indptr = []
All_data = []
#filter
Train_item = range(0,1000)
Train_user = range(0,600000)
Val_item = range(1000,1050)
Val_user = range(0, 600000)
Test_item = range(1050, 1100)
Test_user = range(0, 600000)
for i in range(len(index)):
user_id = index[i]
item_id = indptr[i]
if user_id in user_select and item_id in item_select:
'''
All_index.append(user_select[user_id])
All_indptr.append(item_select[item_id])
All_data.append(data[i])
if user_select[user_id] in Train_user and item_select[item_id] in Train_item:
Train_index.append(user_select[user_id])
Train_indptr.append(item_select[item_id])
Train_data.append(data[i])
elif user_select[user_id] in Test_user and item_select[item_id] in Test_item:
Test_index.append(user_select[user_id])
Test_indptr.append(item_select[item_id])
Test_data.append(data[i])
elif user_select[user_id] in Val_user and item_select[item_id] in Val_item:
Val_index.append(user_select[user_id])
Val_indptr.append(item_select[item_id])
Val_data.append(data[i])
'''
rdn = np.random.randint(0,100)
if rdn < 80:
Train_index.append(user_select[user_id])
Train_indptr.append(item_select[item_id])
Train_data.append(data[i])
elif rdn < 90:
Test_index.append(user_select[user_id])
Test_indptr.append(item_select[item_id])
Test_data.append(data[i])
else:
Val_index.append(user_select[user_id])
Val_indptr.append(item_select[item_id])
Val_data.append(data[i])
num_train_item = len(item_select)
num_train_user = len(user_select)
#num_train_item = len(Train_item) + len(Test_item) + len(Val_item)
#num_train_user = len(Train_user) + len(Test_user) + len(Val_user)
O_train = sp.csr_matrix((np.array(Train_data) + 1, (Train_index, Train_indptr)), shape=(num_train_user, num_train_item))
return O_train, Train_index, Train_indptr, Train_data, Val_index, Val_indptr, Val_data, Test_index,Test_indptr, Test_data, user_select, item_select
def gen_data(file):
user_dic = {}
item_dic = {}
ucount = 0
icount = 0
user_list = []
item_list = []
data = []
pos = []
f = open(file, "r")
for line in f.readlines():
groups = line.strip().split("\t")
if groups[2] not in user_dic:
user_dic[groups[2]] = ucount
ucount += 1
if groups[1] not in item_dic:
item_dic[groups[1]] = icount
icount += 1
pos.append(str(user_dic[groups[2]]) + '-' + str(item_dic[groups[1]]))
user_list.append(user_dic[groups[2]])
item_list.append(item_dic[groups[1]])
data.append(1)
#negtive sampling
d_l = len(data) * 10
for i in range(d_l):
user_id = user_list[0]
item_id = item_list[0]
neg = str(user_id) + '-' + str(item_id)
while neg in pos:
user_id = random.randint(0, ucount-1)
item_id = random.randint(0, icount-1)
neg = str(user_id) + '-' + str(item_id)
pos.append(neg)
user_list.append(user_id)
item_list.append(item_id)
data.append(0)
return user_list, item_list, data, user_dic, item_dic
def load_group_file_rank(path_file):
user, item, data, user_dic, item_dic = gen_data(path_file)
item = np.array(item)
train_data = []
train_user = []
train_item = []
val_data = []
val_user = []
val_item = []
test_data = []
test_user = []
test_item = []
test_dic = {}
degree = {}
for i in range(len(user)):
rdn = random.randint(0, 100)
if rdn > 20:
train_data.append(data[i])
train_user.append(user[i])
train_item.append(item[i])
elif rdn > 10:
test_data.append(data[i])
test_user.append(user[i])
test_item.append(item[i])
if user[i] in test_dic:
test_dic[user[i]].append(item[i])
else:
test_dic[user[i]] = [item[i]]
else:
val_data.append(data[i])
val_user.append(user[i])
val_item.append(item[i])
#add test rank
debug = {}
for user in set(test_user):
item_list = set(train_item) - set(test_dic[user])
debug[user] = []
sample_item = np.random.choice(list(item_list), 500, replace=False)
for item in sample_item:
test_data.append(0)
test_user.append(user)
test_item.append(item)
debug[user].append(item)
#print(debug)
num_train_user = len(user_dic)
num_train_item = len(item_dic)
O_train = sp.csr_matrix((np.array(train_data), (train_user, train_item)), shape=(num_train_user, num_train_item))
return O_train, train_user, train_item, train_data, val_user, val_item, val_data, test_user, test_item, test_data, user_dic, item_dic
def load_group_file(path_file):
user, item, data, user_dic, item_dic = gen_data(path_file)
item = np.array(item)
train_data = []
train_user = []
train_item = []
val_data = []
val_user = []
val_item = []
test_data = []
test_user = []
test_item = []
degree = {}
for i in range(len(user)):
rdn = random.randint(0, 100)
if rdn > 20:
train_data.append(data[i])
train_user.append(user[i])
train_item.append(item[i])
elif rdn > 10:
test_data.append(data[i])
test_user.append(user[i])
test_item.append(item[i])
else:
val_data.append(data[i])
val_user.append(user[i])
val_item.append(item[i])
num_train_user = len(user_dic)
num_train_item = len(item_dic)
O_train = sp.csr_matrix((np.array(train_data), (train_user, train_item)), shape=(num_train_user, num_train_item))
return O_train, train_user, train_item, train_data, val_user, val_item, val_data, test_user, test_item, test_data, user_dic, item_dic
def load_matlab_file(path_file, name_field):
"""
load '.mat' files
inputs:
path_file, string containing the file path
name_field, string containig the field name (default='shape')
warning:
'.mat' files should be saved in the '-v7.3' format
"""
db = h5py.File(path_file, 'r')
ds = db[name_field]
try:
if 'ir' in ds.keys():
data = np.asarray(ds['data'])
ir = np.asarray(ds['ir'])
jc = np.asarray(ds['jc'])
out = sp.csr_matrix((data, ir, jc)).astype(np.float32)
except AttributeError:
# Transpose in case is a dense matrix because of the row- vs column- major ordering between python and matlab
out = np.asarray(ds).astype(np.float32).T
db.close()
return out
def preprocess_user_item_features(u_features, v_features):
"""
Creates one big feature matrix out of user features and item features.
Stacks item features under the user features.
"""
zero_csr_u = sp.csr_matrix((u_features.shape[0], v_features.shape[1]), dtype=u_features.dtype)
zero_csr_v = sp.csr_matrix((v_features.shape[0], u_features.shape[1]), dtype=v_features.dtype)
u_features = sp.hstack([u_features, zero_csr_u], format='csr')
v_features = sp.hstack([zero_csr_v, v_features], format='csr')
return u_features, v_features
def globally_normalize_bipartite_adjacency(adjacencies, verbose=False, symmetric=True):
""" Globally Normalizes set of bipartite adjacency matrices """
if verbose:
print('Symmetrically normalizing bipartite adj')
# degree_u and degree_v are row and column sums of adj+I
adj_tot = np.sum(adj for adj in adjacencies)
degree_u = np.asarray(adj_tot.sum(1)).flatten()
degree_v = np.asarray(adj_tot.sum(0)).flatten()
# set zeros to inf to avoid dividing by zero
degree_u[degree_u == 0.] = np.inf
degree_v[degree_v == 0.] = np.inf
degree_u_inv_sqrt = 1. / np.sqrt(degree_u)
degree_v_inv_sqrt = 1. / np.sqrt(degree_v)
degree_u_inv_sqrt_mat = sp.diags([degree_u_inv_sqrt], [0])
degree_v_inv_sqrt_mat = sp.diags([degree_v_inv_sqrt], [0])
degree_u_inv = degree_u_inv_sqrt_mat.dot(degree_u_inv_sqrt_mat)
if symmetric:
adj_norm = [degree_u_inv_sqrt_mat.dot(adj).dot(degree_v_inv_sqrt_mat) for adj in adjacencies]
else:
adj_norm = [degree_u_inv.dot(adj) for adj in adjacencies]
return adj_norm
def sparse_to_tuple(sparse_mx):
""" change of format for sparse matrix. This format is used
for the feed_dict where sparse matrices need to be linked to placeholders
representing sparse matrices. """
if not sp.isspmatrix_coo(sparse_mx):
sparse_mx = sparse_mx.tocoo()
coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
values = sparse_mx.data
shape = sparse_mx.shape
return coords, values, shape
def create_trainvaltest_split(dataset, seed=1234, testing=False, datasplit_path=None, datasplit_from_file=False, verbose=True, rating_map=None, post_rating_map=None, ratio=1.0):
"""
Splits data set into train/val/test sets from full bipartite adjacency matrix. Shuffling of dataset is done in
load_data function.
For each split computes 1-of-num_classes labels. Also computes training
adjacency matrix.
"""
if datasplit_from_file and os.path.isfile(datasplit_path):
print('Reading dataset splits from file...')
with open(datasplit_path, 'rb') as f:
num_users, num_items, u_nodes, v_nodes, ratings, u_features, v_features = pkl.load(f)
if verbose:
print('Number of users = %d' % num_users)
print('Number of items = %d' % num_items)
print('Number of links = %d' % ratings.shape[0])
print('Fraction of positive links = %.4f' % (float(ratings.shape[0]) / (num_users * num_items),))
else:
num_users, num_items, u_nodes, v_nodes, ratings, u_features, v_features = load_data(dataset, seed=seed,
verbose=verbose)
with open(datasplit_path, 'wb') as f:
pkl.dump([num_users, num_items, u_nodes, v_nodes, ratings, u_features, v_features], f)
if rating_map is not None:
for i, x in enumerate(ratings):
ratings[i] = rating_map[x]
neutral_rating = -1
rating_dict = {r: i for i, r in enumerate(np.sort(np.unique(ratings)).tolist())}
labels = np.full((num_users, num_items), neutral_rating, dtype=np.int32)
labels[u_nodes, v_nodes] = np.array([rating_dict[r] for r in ratings])
labels = labels.reshape([-1])
# number of test and validation edges
num_test = int(np.ceil(ratings.shape[0] * 0.1))
if dataset == 'ml_100k':
num_val = int(np.ceil(ratings.shape[0] * 0.9 * 0.05))
else:
num_val = int(np.ceil(ratings.shape[0] * 0.9 * 0.05))
num_train = ratings.shape[0] - num_val - num_test
pairs_nonzero = np.array([[u, v] for u, v in zip(u_nodes, v_nodes)])
idx_nonzero = np.array([u * num_items + v for u, v in pairs_nonzero])
train_idx = idx_nonzero[0:int(num_train*ratio)]
val_idx = idx_nonzero[num_train:num_train + num_val]
test_idx = idx_nonzero[num_train + num_val:]
train_pairs_idx = pairs_nonzero[0:int(num_train*ratio)]
val_pairs_idx = pairs_nonzero[num_train:num_train + num_val]
test_pairs_idx = pairs_nonzero[num_train + num_val:]
u_test_idx, v_test_idx = test_pairs_idx.transpose()
u_val_idx, v_val_idx = val_pairs_idx.transpose()
u_train_idx, v_train_idx = train_pairs_idx.transpose()
# create labels
train_labels = labels[train_idx]
val_labels = labels[val_idx]
test_labels = labels[test_idx]
if testing:
u_train_idx = np.hstack([u_train_idx, u_val_idx])
v_train_idx = np.hstack([v_train_idx, v_val_idx])
train_labels = np.hstack([train_labels, val_labels])
# for adjacency matrix construction
train_idx = np.hstack([train_idx, val_idx])
class_values = np.sort(np.unique(ratings))
# make training adjacency matrix
rating_mx_train = np.zeros(num_users * num_items, dtype=np.float32)
if post_rating_map is None:
rating_mx_train[train_idx] = labels[train_idx].astype(np.float32) + 1.
else:
rating_mx_train[train_idx] = np.array([post_rating_map[r] for r in class_values[labels[train_idx]]]) + 1.
rating_mx_train = sp.csr_matrix(rating_mx_train.reshape(num_users, num_items))
return u_features, v_features, rating_mx_train, train_labels, u_train_idx, v_train_idx, \
val_labels, u_val_idx, v_val_idx, test_labels, u_test_idx, v_test_idx, class_values
def load_data_monti_filter(dataset, testing=False, rating_map=None, post_rating_map=None, own = False):
"""
Loads data from Monti et al. paper.
if rating_map is given, apply this map to the original rating matrix
if post_rating_map is given, apply this map to the processed rating_mx_train without affecting the labels
"""
if not own:
path_dataset = 'raw_data/' + dataset + '/training_test_dataset.mat'
M = load_matlab_file(path_dataset, 'M')
if rating_map is not None:
M[np.where(M)] = [rating_map[x] for x in M[np.where(M)]]
print(M.shape)
Otraining = load_matlab_file(path_dataset, 'Otraining')
# filter of dataset
user_median = np.median(np.sum(Otraining, axis = 0))
item_median = np.median(np.sum(Otraining, axis = 1))
user_id = np.arange(Otraining.shape[0]) + 1
item_id = np.arange(Otraining.shape[1]) + 1
keep_user = np.where(np.sum(Otraining, axis = 0) < user_median * 1 + 1, user_id, np.zeros_like(user_id))
keep_item = np.where(np.sum(Otraining, axis = 1) < item_median * 1 + 1, item_id, np.zeros_like(item_id))
keep_user = keep_user[ keep_user != 0]
keep_item = keep_item[keep_item != 0]
'''
Otraining = Otraining[:, keep_user - 1]
Otraining = Otraining[keep_item - 1, :]
user_median = np.min(np.sum(Otraining, axis = 0))
item_median = np.min(np.sum(Otraining, axis = 1))
'''
print(user_median * 3)
print(item_median * 3)
Otest = load_matlab_file(path_dataset, 'Otest')
'''
Otest = Otest[:, keep_user - 1]
Otest = Otest[keep_item - 1, :]
#M = M[:, keep_user - 1]
#M = M[keep_item - 1, :]
Otest[:, keep_user - 1] = 0
Otest[keep_item - 1, :] = 0
num_users = Otraining.shape[0]
num_items = Otraining.shape[1]
'''
else:
path_dataset = 'raw_data/' + dataset + '/douban_test'
print(path_dataset)
if dataset == 'flixster':
Wrow = load_matlab_file(path_dataset, 'W_users')
Wcol = load_matlab_file(path_dataset, 'W_movies')
u_features = Wrow
v_features = Wcol
elif dataset == 'douban':
Wrow = load_matlab_file(path_dataset, 'W_users')
#u_features = Wrow
#v_features = np.eye(num_items)
elif dataset == 'yahoo_music':
Wcol = load_matlab_file(path_dataset, 'W_tracks')
u_features = np.eye(num_users)
v_features = Wcol
elif dataset == 'own' or dataset == 'all':
u_features = None
v_features = None
rating_train, Train_index, Train_indptr, Train_data, Val_index, Val_indptr, Val_data, Test_index,Test_indptr, Test_data, user_dic, item_dic = load_own_file2(path_dataset)
Train_data = np.array(Train_data, dtype = np.int32)
Train_index = np.array(Train_index)
Train_indptr = np.array(Train_indptr)
Val_data = np.array(Val_data, dtype = np.int32)
Val_index = np.array(Val_index)
Val_indptr = np.array(Val_indptr)
Test_data = np.array(Test_data, dtype = np.int32)
Test_index = np.array(Test_index)
Test_indptr = np.array(Test_indptr)
class_values = np.array([1, 2, 3, 4, 5])
print('number of users = ', len(user_dic))
print('number of item = ', len(item_dic))
print("train_labels:")
print(Train_data)
print("u_train_idx")
print(Train_index)
print("v_train_idx")
print(Train_indptr)
print("test_labels")
print(Test_data)
print("u_test_idx")
print(Test_index)
print("v_test_idx")
print(Test_indptr)
print("class_values")
print(class_values)
return u_features, v_features, rating_train, Train_data, Train_index, Train_indptr, \
Val_data, Val_index, Val_indptr, Test_data, Test_index, Test_indptr, class_values
u_nodes_ratings = np.where(M)[0]
v_nodes_ratings = np.where(M)[1]
ratings = M[np.where(M)]
u_nodes_ratings, v_nodes_ratings = u_nodes_ratings.astype(np.int64), v_nodes_ratings.astype(np.int32)
ratings = ratings.astype(np.float64)
u_nodes = u_nodes_ratings
v_nodes = v_nodes_ratings
print('number of users = ', len(set(u_nodes)))
print('number of item = ', len(set(v_nodes)))
u_features = np.array(range(num_users)).reshape((-1, 1))
v_features = np.array(range(num_users, num_users + num_items)).reshape(-1, 1)
neutral_rating = -1 # int(np.ceil(np.float(num_classes)/2.)) - 1
# assumes that ratings_train contains at least one example of every rating type
rating_dict = {r: i for i, r in enumerate(np.sort(np.unique(ratings)).tolist())}
print(num_users)
print(num_items)
labels = np.full((num_users, num_items), neutral_rating, dtype=np.int32)
labels[u_nodes, v_nodes] = np.array([rating_dict[r] for r in ratings])
for i in range(len(u_nodes)):
assert(labels[u_nodes[i], v_nodes[i]] == rating_dict[ratings[i]])
labels = labels.reshape([-1])
# number of test and validation edges
num_train = np.where(Otraining)[0].shape[0]
num_test = np.where(Otest)[0].shape[0]
num_val = int(np.ceil(num_train * 0.2))
num_train = num_train - num_val
pairs_nonzero_train = np.array([[u, v] for u, v in zip(np.where(Otraining)[0], np.where(Otraining)[1])])
idx_nonzero_train = np.array([u * num_items + v for u, v in pairs_nonzero_train])
pairs_nonzero_test = np.array([[u, v] for u, v in zip(np.where(Otest)[0], np.where(Otest)[1])])
idx_nonzero_test = np.array([u * num_items + v for u, v in pairs_nonzero_test])
# Internally shuffle training set (before splitting off validation set)
rand_idx = list(range(len(idx_nonzero_train)))
np.random.seed(42)
np.random.shuffle(rand_idx)
idx_nonzero_train = idx_nonzero_train[rand_idx]
pairs_nonzero_train = pairs_nonzero_train[rand_idx]
idx_nonzero = np.concatenate([idx_nonzero_train, idx_nonzero_test], axis=0)
pairs_nonzero = np.concatenate([pairs_nonzero_train, pairs_nonzero_test], axis=0)
val_idx = idx_nonzero[0:num_val]
train_idx = idx_nonzero[num_val:num_train + num_val]
test_idx = idx_nonzero[num_train + num_val:]
assert(len(test_idx) == num_test)
val_pairs_idx = pairs_nonzero[0:num_val]
train_pairs_idx = pairs_nonzero[num_val:num_train + num_val]
test_pairs_idx = pairs_nonzero[num_train + num_val:]
u_test_idx, v_test_idx = test_pairs_idx.transpose()
u_val_idx, v_val_idx = val_pairs_idx.transpose()
u_train_idx, v_train_idx = train_pairs_idx.transpose()
# create labels
train_labels = labels[train_idx]
val_labels = labels[val_idx]
test_labels = labels[test_idx]
if testing:
u_train_idx = np.hstack([u_train_idx, u_val_idx])
v_train_idx = np.hstack([v_train_idx, v_val_idx])
train_labels = np.hstack([train_labels, val_labels])
# for adjacency matrix construction
train_idx = np.hstack([train_idx, val_idx])
class_values = np.sort(np.unique(ratings))
# make training adjacency matrix
rating_mx_train = np.zeros(num_users * num_items, dtype=np.float32)
'''Note here rating matrix elements' values + 1 !!!'''
if post_rating_map is None:
rating_mx_train[train_idx] = labels[train_idx].astype(np.float32) + 1.
else:
rating_mx_train[train_idx] = np.array([post_rating_map[r] for r in class_values[labels[train_idx]]]) + 1.
rating_mx_train = sp.csr_matrix(rating_mx_train.reshape(num_users, num_items))
if u_features is not None:
print("user Features:")
print(u_features)
u_features = sp.csr_matrix(u_features)
print("User features shape: " + str(u_features.shape))
if v_features is not None:
print("Item Features")
print(v_features)
v_features = sp.csr_matrix(v_features)
print("Item features shape: " + str(v_features.shape))
print("train_labels:")
print(train_labels)
print("u_train_idx")
print(u_train_idx)
print("v_train_idx")
print(v_train_idx)
print("test_labels")
print(test_labels)
print("u_test_idx")
print(u_test_idx)
print("v_test_idx")
print(v_test_idx)
print("class_values")
print(class_values)
return u_features, v_features, rating_mx_train, train_labels, u_train_idx, v_train_idx, \
val_labels, u_val_idx, v_val_idx, test_labels, u_test_idx, v_test_idx, class_values, num_users
def load_data_monti(dataset, testing=False, rating_map=None, post_rating_map=None, own = False):
"""
Loads data from Monti et al. paper.
if rating_map is given, apply this map to the original rating matrix
if post_rating_map is given, apply this map to the processed rating_mx_train without affecting the labels
"""
if not own:
path_dataset = 'raw_data/' + dataset + '/training_test_dataset.mat'
M = load_matlab_file(path_dataset, 'M')
if rating_map is not None:
M[np.where(M)] = [rating_map[x] for x in M[np.where(M)]]
print(M.shape)
Otraining = load_matlab_file(path_dataset, 'Otraining')
Otest = load_matlab_file(path_dataset, 'Otest')
num_users = M.shape[0]
num_items = M.shape[1]
else:
path_dataset = 'raw_data/' + dataset + '/douban_train'
print(path_dataset)
if dataset == 'flixster':
Wrow = load_matlab_file(path_dataset, 'W_users')
Wcol = load_matlab_file(path_dataset, 'W_movies')
u_features = Wrow
v_features = Wcol
elif dataset == 'douban':
Wrow = load_matlab_file(path_dataset, 'W_users')
u_features = Wrow
v_features = np.eye(num_items)
elif dataset == 'yahoo_music':
Wcol = load_matlab_file(path_dataset, 'W_tracks')
u_features = np.eye(num_users)
v_features = Wcol
elif dataset == 'own' or dataset == 'all':
u_features = None
v_features = None
rating_train, Train_index, Train_indptr, Train_data, Val_index, Val_indptr, Val_data, Test_index,Test_indptr, Test_data, user_dic, item_dic = load_own_file2(path_dataset)
Train_indptr = list(np.array(Train_indptr) + len(user_dic))
Val_indptr = list(np.array(Val_indptr) + len(user_dic))
Test_indptr = list(np.array(Test_indptr) + len(user_dic))
class_values = np.array([1, 2, 3, 4, 5])
print('number of users = ', len(user_dic))
print('number of item = ', len(item_dic))
print("train_labels:")
print(Train_data)
print("u_train_idx")
print(Train_index)
print("v_train_idx")
print(Train_indptr)
print("test_labels")
print(Test_data)
print("u_test_idx")
print(Test_index)
print("v_test_idx")
print(Test_indptr)
print("class_values")
print(class_values)
return u_features, v_features, rating_train, Train_data, Train_index, Train_indptr, \
Val_data, Val_index, Val_indptr, Test_data, Test_index, Test_indptr, class_values
elif dataset == 'group':
rating_train, Train_index, Train_indptr, Train_data, Val_index, Val_indptr, Val_data, Test_index,Test_indptr, Test_data, user_dic, item_dic = load_group_file_rank(path_dataset)
u_features = range(len(user_dic))
v_features = range(len(user_dic), len(item_dic)+len(user_dic))
Train_indptr = list(np.array(Train_indptr) + len(user_dic))
Val_indptr = list(np.array(Val_indptr) + len(user_dic))
Test_indptr = list(np.array(Test_indptr) + len(user_dic))
class_values = np.array([0, 1])
print('number of users = ', len(user_dic))
print('number of item = ', len(item_dic))
return u_features, v_features, rating_train, Train_data, Train_index, Train_indptr, \
Val_data, Val_index, Val_indptr, Test_data, Test_index, Test_indptr, class_values
u_nodes_ratings = np.where(M)[0]
v_nodes_ratings = np.where(M)[1]
print("u_nodes:")
print(u_nodes_ratings)
print("v_nodes:")
print(v_nodes_ratings)
ratings = M[np.where(M)]
'''
#Test SVD
U, s, Vh = linalg.svds(Otraining)
s_diag_matrix = np.diag(s)
svd_prediction = np.dot(np.dot(U,s_diag_matrix),Vh)
prediction_flatten = np.reshape(svd_prediction[Otest.nonzero()], (1,-1))
test_data_matrix_flatten = Otest[Otest.nonzero()]
rmse = sqrt(mean_squared_error(prediction_flatten,test_data_matrix_flatten))
print("SVD rmse:", rmse)
'''
u_nodes_ratings, v_nodes_ratings = u_nodes_ratings.astype(np.int64), v_nodes_ratings.astype(np.int32)
ratings = ratings.astype(np.float64)
u_nodes = u_nodes_ratings
v_nodes = v_nodes_ratings
print('number of users = ', len(set(u_nodes)))
print('number of item = ', len(set(v_nodes)))
neutral_rating = -1 # int(np.ceil(np.float(num_classes)/2.)) - 1
# assumes that ratings_train contains at least one example of every rating type
rating_dict = {r: i for i, r in enumerate(np.sort(np.unique(ratings)).tolist())}
labels = np.full((num_users, num_items), neutral_rating, dtype=np.int32)
labels[u_nodes, v_nodes] = np.array([rating_dict[r] for r in ratings])
for i in range(len(u_nodes)):
assert(labels[u_nodes[i], v_nodes[i]] == rating_dict[ratings[i]])
labels = labels.reshape([-1])
# number of test and validation edges
num_train = np.where(Otraining)[0].shape[0]
num_test = np.where(Otest)[0].shape[0]
num_val = int(np.ceil(num_train * 0.2))
num_train = num_train - num_val
pairs_nonzero_train = np.array([[u, v] for u, v in zip(np.where(Otraining)[0], np.where(Otraining)[1])])
idx_nonzero_train = np.array([u * num_items + v for u, v in pairs_nonzero_train])
pairs_nonzero_test = np.array([[u, v] for u, v in zip(np.where(Otest)[0], np.where(Otest)[1])])
idx_nonzero_test = np.array([u * num_items + v for u, v in pairs_nonzero_test])
# Internally shuffle training set (before splitting off validation set)
rand_idx = list(range(len(idx_nonzero_train)))
np.random.seed(42)
np.random.shuffle(rand_idx)
idx_nonzero_train = idx_nonzero_train[rand_idx]
pairs_nonzero_train = pairs_nonzero_train[rand_idx]
idx_nonzero = np.concatenate([idx_nonzero_train, idx_nonzero_test], axis=0)
pairs_nonzero = np.concatenate([pairs_nonzero_train, pairs_nonzero_test], axis=0)
val_idx = idx_nonzero[0:num_val]
train_idx = idx_nonzero[num_val:num_train + num_val]
test_idx = idx_nonzero[num_train + num_val:]
assert(len(test_idx) == num_test)
val_pairs_idx = pairs_nonzero[0:num_val]
train_pairs_idx = pairs_nonzero[num_val:num_train + num_val]
test_pairs_idx = pairs_nonzero[num_train + num_val:]
u_test_idx, v_test_idx = test_pairs_idx.transpose()
u_val_idx, v_val_idx = val_pairs_idx.transpose()
u_train_idx, v_train_idx = train_pairs_idx.transpose()
# create labels
train_labels = labels[train_idx]
val_labels = labels[val_idx]
test_labels = labels[test_idx]
if testing:
u_train_idx = np.hstack([u_train_idx, u_val_idx])
v_train_idx = np.hstack([v_train_idx, v_val_idx])
train_labels = np.hstack([train_labels, val_labels])
# for adjacency matrix construction
train_idx = np.hstack([train_idx, val_idx])
class_values = np.sort(np.unique(ratings))
# make training adjacency matrix
rating_mx_train = np.zeros(num_users * num_items, dtype=np.float32)
'''Note here rating matrix elements' values + 1 !!!'''
if post_rating_map is None:
rating_mx_train[train_idx] = labels[train_idx].astype(np.float32) + 1.
else:
rating_mx_train[train_idx] = np.array([post_rating_map[r] for r in class_values[labels[train_idx]]]) + 1.
rating_mx_train = sp.csr_matrix(rating_mx_train.reshape(num_users, num_items))
if u_features is not None:
print("user Features:")
print(u_features)
u_features = sp.csr_matrix(u_features)
print("User features shape: " + str(u_features.shape))
if v_features is not None:
print("Item Features")
print(v_features)
v_features = sp.csr_matrix(v_features)
print("Item features shape: " + str(v_features.shape))
print("train_labels:")
print(train_labels)
print("u_train_idx")
print(u_train_idx)
print("v_train_idx")
print(v_train_idx)
print("test_labels")
print(test_labels)
print("u_test_idx")
print(u_test_idx)
print("v_test_idx")
print(v_test_idx)
print("class_values")
print(class_values)
print(num_users)
return u_features, v_features, rating_mx_train, train_labels, u_train_idx, v_train_idx, \
val_labels, u_val_idx, v_val_idx, test_labels, u_test_idx, v_test_idx, class_values, num_users
def load_official_trainvaltest_split(dataset, testing=False, rating_map=None, post_rating_map=None, ratio=1.0):
"""
Loads official train/test split and uses 10% of training samples for validaiton
For each split computes 1-of-num_classes labels. Also computes training
adjacency matrix. Assumes flattening happens everywhere in row-major fashion.
"""
sep = '\t'
# Check if files exist and download otherwise
files = ['/u1.base', '/u1.test', '/u.item', '/u.user']
fname = dataset
data_dir = 'raw_data/' + fname
download_dataset(fname, files, data_dir)
dtypes = {
'u_nodes': np.int32, 'v_nodes': np.int32,
'ratings': np.float32, 'timestamp': np.float64}
filename_train = 'raw_data/' + dataset + '/u1.base'
filename_test = 'raw_data/' + dataset + '/u1.test'
data_train = pd.read_csv(
filename_train, sep=sep, header=None,
names=['u_nodes', 'v_nodes', 'ratings', 'timestamp'], dtype=dtypes)
data_test = pd.read_csv(
filename_test, sep=sep, header=None,
names=['u_nodes', 'v_nodes', 'ratings', 'timestamp'], dtype=dtypes)
data_array_train = data_train.values.tolist()
data_array_train = np.array(data_array_train)
data_array_test = data_test.values.tolist()
data_array_test = np.array(data_array_test)
if ratio < 1.0:
data_array_train = data_array_train[data_array_train[:, -1].argsort()[:int(ratio*len(data_array_train))]]
data_array = np.concatenate([data_array_train, data_array_test], axis=0)
u_nodes_ratings = data_array[:, 0].astype(dtypes['u_nodes'])
v_nodes_ratings = data_array[:, 1].astype(dtypes['v_nodes'])
ratings = data_array[:, 2].astype(dtypes['ratings'])
if rating_map is not None:
for i, x in enumerate(ratings):
ratings[i] = rating_map[x]
u_nodes_ratings, u_dict, num_users = map_data(u_nodes_ratings)
v_nodes_ratings, v_dict, num_items = map_data(v_nodes_ratings)
u_nodes_ratings, v_nodes_ratings = u_nodes_ratings.astype(np.int64), v_nodes_ratings.astype(np.int32)
ratings = ratings.astype(np.float64)
u_nodes = u_nodes_ratings
v_nodes = v_nodes_ratings
neutral_rating = -1 # int(np.ceil(np.float(num_classes)/2.)) - 1
# assumes that ratings_train contains at least one example of every rating type
rating_dict = {r: i for i, r in enumerate(np.sort(np.unique(ratings)).tolist())}
labels = np.full((num_users, num_items), neutral_rating, dtype=np.int32)
labels[u_nodes, v_nodes] = np.array([rating_dict[r] for r in ratings])
for i in range(len(u_nodes)):
assert(labels[u_nodes[i], v_nodes[i]] == rating_dict[ratings[i]])
labels = labels.reshape([-1])
# number of test and validation edges, see cf-nade code
num_train = data_array_train.shape[0]
num_test = data_array_test.shape[0]
num_val = int(np.ceil(num_train * 0.2))
num_train = num_train - num_val
pairs_nonzero = np.array([[u, v] for u, v in zip(u_nodes, v_nodes)])
idx_nonzero = np.array([u * num_items + v for u, v in pairs_nonzero])