-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.py
949 lines (748 loc) · 38.4 KB
/
data.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
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
"""MovieLens dataset"""
import os
import scipy.sparse as sp
import numpy as np
import pandas as pd
import torch as th
import dgl
from dgl.data.utils import download, extract_archive, get_download_dir
from refex import extract_refex_feature
import utils
_urls = {
'ml-100k' : 'http://files.grouplens.org/datasets/movielens/ml-100k.zip',
'ml-1m' : 'http://files.grouplens.org/datasets/movielens/ml-1m.zip',
}
GENRES_ML_100K =\
['unknown', 'Action', 'Adventure', 'Animation',
'Children', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy',
'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi',
'Thriller', 'War', 'Western']
GENRES_ML_1M = GENRES_ML_100K[1:]
class MovieLens(object):
"""MovieLens dataset used by GCMC model
"""
def __init__(self, data_name, testing=False,
test_ratio=0.1, valid_ratio=0.2):
# self._data_name = data_name
# # 1. download and extract
# download_dir = get_download_dir()
# self._dir = os.path.join(download_dir, data_name, data_name)
# if not os.path.exists(self._dir):
# zip_file_path = '{}/{}.zip'.format(download_dir, data_name)
# download(_urls[data_name], path=zip_file_path)
# extract_archive(zip_file_path, '{}/{}'.format(download_dir, data_name))
# print("Starting processing {} ...".format(self._data_name))
# # 2. load rating data
# if self._data_name == 'ml-100k':
# train_rating_data = self._load_raw_rates(os.path.join(self._dir, 'u1.base'), '\t')
# test_rating_data = self._load_raw_rates(os.path.join(self._dir, 'u1.test'), '\t')
# all_rating_data = pd.concat([train_rating_data, test_rating_data])
# elif self._data_name == 'ml-1m':
# all_rating_data = self._load_raw_rates(os.path.join(self._dir, 'ratings.dat'), '::')
# num_test = int(np.ceil(all_rating_data.shape[0] * test_ratio))
# shuffled_idx = np.random.permutation(all_rating_data.shape[0])
# test_rating_data = all_rating_data.iloc[shuffled_idx[: num_test]]
# train_rating_data = all_rating_data.iloc[shuffled_idx[num_test: ]]
# else:
# raise NotImplementedError
# num_valid = int(np.ceil(train_rating_data.shape[0] * valid_ratio))
# shuffled_idx = np.random.permutation(train_rating_data.shape[0])
# valid_rating_data = train_rating_data.iloc[shuffled_idx[: num_valid]]
# if not testing:
# train_rating_data = train_rating_data.iloc[shuffled_idx[num_valid:]]
# self._rating = np.sort(np.unique(all_rating_data["rating"].values))
# print("All rating pairs : {}".format(all_rating_data.shape[0]))
# # print("\tAll train rating pairs : {}".format(self.all_train_rating_data.shape[0]))
# print("\tTrain rating pairs : {}".format(train_rating_data.shape[0]))
# print("\tValid rating pairs : {}".format(valid_rating_data.shape[0]))
# print("\tTest rating pairs : {}".format(test_rating_data.shape[0]))
# # 2. load user and movie data, and drop those unseen in rating_data
# user_data = self._load_raw_user_data()
# movie_data = self._load_raw_movie_data()
# user_data = self._drop_unseen_nodes(data_df=user_data,
# col_name="id",
# reserved_ids_set=set(all_rating_data["user_id"].values))
# movie_data = self._drop_unseen_nodes(data_df=movie_data,
# col_name="id",
# reserved_ids_set=set(all_rating_data["movie_id"].values))
# # 3. set user and movie feature to None
# user_feature = None
# movie_feature = None
# # 4. generate rating pairs
# # Map user/movie to the global id
# self._global_user_id_map = {ele: i for i, ele in enumerate(user_data['id'])}
# self._global_movie_id_map = {ele: i for i, ele in enumerate(movie_data['id'])}
# print('Total user number = {}, movie number = {}'.format(len(self._global_user_id_map),
# len(self._global_movie_id_map)))
# self._num_user = len(self._global_user_id_map)
# self._num_movie = len(self._global_movie_id_map)
# # pair value is idx rather than id, and rating value starts from 1.0
# # self.all_train_rating_pairs, self.all_train_rating_values = self._generate_pair_value(self.all_train_rating_data)
# train_u_indices, train_v_indices, train_labels = self._generate_pair_value(train_rating_data)
# val_u_indices, val_v_indices, val_labels = self._generate_pair_value(valid_rating_data)
# test_u_indices, test_v_indices, test_labels = self._generate_pair_value(test_rating_data)
if data_name == 'ml-100k':
print("Using official MovieLens dataset split u1.base/u1.test with 20% validation set size...")
(
u_features, v_features, adj_train, train_labels, train_u_indices, train_v_indices,
val_labels, val_u_indices, val_v_indices, test_labels, test_u_indices,
test_v_indices, class_values
) = load_official_trainvaltest_split(
'ml-100k', testing, None, None, 1.0
)
elif data_name == 'ml-1m':
data_seed = 1234
datasplit_path = (
'raw_data/' + data_name + '/split_seed' + str(data_seed) +
'.pickle'
)
print("Using random dataset split ...")
(
u_features, v_features, adj_train, train_labels, train_u_indices, train_v_indices,
val_labels, val_u_indices, val_v_indices, test_labels, test_u_indices,
test_v_indices, class_values
) = create_trainvaltest_split(
'ml-1m', 1234, testing, datasplit_path, True, True, None,
None, 1.0
)
self._num_user = u_features.shape[0]
self._num_movie = v_features.shape[0]
# reindex u and v, v nodes start after u
train_v_indices += self.num_user
val_v_indices += self.num_user
test_v_indices += self.num_user
self.train_rating_pairs = (th.LongTensor(train_u_indices), th.LongTensor(train_v_indices))
self.valid_rating_pairs = (th.LongTensor(val_u_indices), th.LongTensor(val_v_indices))
self.test_rating_pairs = (th.LongTensor(test_u_indices), th.LongTensor(test_v_indices))
self.train_rating_values = th.FloatTensor(train_labels)
self.valid_rating_values = th.FloatTensor(val_labels)
self.test_rating_values = th.FloatTensor(test_labels)
print("\tTrain rating pairs : {}".format(len(train_labels)))
print("\tValid rating pairs : {}".format(len(val_labels)))
print("\tTest rating pairs : {}".format(len(test_labels)))
# build dgl graph object, which is homogeneous and bidirectional and contains only training edges
self.train_graph = dgl.graph((th.cat([self.train_rating_pairs[0], self.train_rating_pairs[1]]),
th.cat([self.train_rating_pairs[1], self.train_rating_pairs[0]])))
self.train_graph.edata['etype'] = th.cat([self.train_rating_values, self.train_rating_values]).to(th.long)
# # add refex feature
# refex_feature = extract_refex_feature(self.train_graph)
# print("refex feature shape: {}".format(refex_feature.numpy().shape))
# self.train_graph.ndata['refex'] = refex_feature
# # add gdv feature
# gdv_feature = np.loadtxt('./{}.gdv'.format(data_name), dtype=np.float32)
# print("gdv feature shape: {}".format(gdv_feature.shape))
# gdv_feature = utils.MinMaxScaling(gdv_feature, axis=0)
# self.train_graph.ndata['gdv'] = th.from_numpy(gdv_feature)
@property
def num_rating(self):
return self._rating.size
@property
def num_user(self):
return self._num_user
@property
def num_movie(self):
return self._num_movie
def _load_raw_user_data(self):
"""In MovieLens, the user attributes file have the following formats:
ml-100k:
user id | age | gender | occupation | zip code
ml-1m:
UserID::Gender::Age::Occupation::Zip-code
Parameters
----------
name : str
Returns
-------
user_data : pd.DataFrame
"""
if self._data_name == 'ml-100k':
user_data = pd.read_csv(os.path.join(self._dir, 'u.user'), sep='|', header=None,
names=['id', 'age', 'gender', 'occupation', 'zip_code'], engine='python')
elif self._data_name == 'ml-1m':
user_data = pd.read_csv(os.path.join(self._dir, 'users.dat'), sep='::', header=None,
names=['id', 'gender', 'age', 'occupation', 'zip_code'], engine='python')
else:
raise NotImplementedError
return user_data
def _load_raw_movie_data(self):
"""In MovieLens, the movie attributes may have the following formats:
In ml-100k:
movie id | movie title | release date | video release date | IMDb URL | [genres]
In ml-1m, ml-10m:
MovieID::Title (Release Year)::Genres
Also, Genres are separated by |, e.g., Adventure|Animation|Children|Comedy|Fantasy
Parameters
----------
name : str
Returns
-------
movie_data : pd.DataFrame
For ml-100k, the column name is ['id', 'title', 'release_date', 'video_release_date', 'url'] + [GENRES (19)]]
For ml-1m, the column name is ['id', 'title'] + [GENRES (18/20)]]
"""
if self._data_name == 'ml-100k':
GENRES = GENRES_ML_100K
elif self._data_name == 'ml-1m':
GENRES = GENRES_ML_1M
else:
raise NotImplementedError
if self._data_name == 'ml-100k':
file_path = os.path.join(self._dir, 'u.item')
movie_data = pd.read_csv(file_path, sep='|', header=None,
names=['id', 'title', 'release_date', 'video_release_date', 'url'] + GENRES,
engine='python')
elif self._data_name == 'ml-1m':
file_path = os.path.join(self._dir, 'movies.dat')
movie_data = pd.read_csv(file_path, sep='::', header=None,
names=['id', 'title', 'genres'], engine='python')
genre_map = {ele: i for i, ele in enumerate(GENRES)}
genre_map['Children\'s'] = genre_map['Children']
genre_map['Childrens'] = genre_map['Children']
movie_genres = np.zeros(shape=(movie_data.shape[0], len(GENRES)), dtype=np.float32)
for i, genres in enumerate(movie_data['genres']):
for ele in genres.split('|'):
if ele in genre_map:
movie_genres[i, genre_map[ele]] = 1.0
else:
print('genres not found, filled with unknown: {}'.format(genres))
movie_genres[i, genre_map['unknown']] = 1.0
for idx, genre_name in enumerate(GENRES):
assert idx == genre_map[genre_name]
movie_data[genre_name] = movie_genres[:, idx]
movie_data = movie_data.drop(columns=["genres"])
else:
raise NotImplementedError
return movie_data
def _load_raw_rates(self, file_path, sep):
"""In MovieLens, the rates have the following format
ml-100k
user id \t movie id \t rating \t timestamp
ml-1m/10m
UserID::MovieID::Rating::Timestamp
timestamp is unix timestamp and can be converted by pd.to_datetime(X, unit='s')
Parameters
----------
file_path : str
Returns
-------
rating_data : pd.DataFrame
"""
rating_data = pd.read_csv(
file_path, sep=sep, header=None,
names=['user_id', 'movie_id', 'rating', 'timestamp'],
dtype={'user_id': np.int32, 'movie_id' : np.int32,
'ratings': np.float32, 'timestamp': np.int64}, engine='python')
return rating_data
def _drop_unseen_nodes(self, data_df, col_name, reserved_ids_set):
data_df = data_df[data_df[col_name].isin(reserved_ids_set)]
data_df.reset_index(drop=True, inplace=True)
return data_df
def _generate_pair_value(self, rating_data):
rating_pairs = (np.array([self._global_user_id_map[ele] for ele in rating_data["user_id"]],
dtype=np.int32),
np.array([self._global_movie_id_map[ele] for ele in rating_data["movie_id"]],
dtype=np.int32))
# label ranges from 0. to 4.
rating_values = rating_data["rating"].values.astype(np.float32) - 1.
return rating_pairs[0], rating_pairs[1], rating_values
import os
import random
import pickle as pkl
import pandas as pd
import numpy as np
import scipy.sparse as sp
# For automatic dataset downloading
from urllib.request import urlopen
from zipfile import ZipFile
from io import BytesIO
def map_data(data):
"""
Map data to proper indices in case they are not in a continues [0, N) range
Parameters
----------
data : np.int32 arrays
Returns
-------
mapped_data : np.int32 arrays
n : length of mapped_data
"""
uniq = list(set(data))
id_dict = {old: new for new, old in enumerate(sorted(uniq))}
data = np.array([id_dict[x] for x in data])
n = len(uniq)
return data, id_dict, n
def download_dataset(dataset, files, data_dir):
""" Downloads dataset if files are not present. """
if not np.all([os.path.isfile(data_dir + f) for f in files]):
url = "http://files.grouplens.org/datasets/movielens/" + dataset.replace('_', '-') + '.zip'
request = urlopen(url)
print('Downloading %s dataset' % dataset)
if dataset in ['ml-100k', 'ml-1m']:
target_dir = 'raw_data/' + dataset.replace('_', '-')
elif dataset == 'ml-10m':
target_dir = 'raw_data/' + 'ml-10M100K'
else:
raise ValueError('Invalid dataset option %s' % dataset)
with ZipFile(BytesIO(request.read())) as zip_ref:
zip_ref.extractall('raw_data/')
os.rename(target_dir, data_dir)
#shutil.rmtree(target_dir)
def load_data(fname, seed=1234, verbose=True):
""" Loads dataset and creates adjacency matrix
and feature matrix
Parameters
----------
fname : str, dataset
seed: int, dataset shuffling seed
verbose: to print out statements or not
Returns
-------
num_users : int
Number of users and items respectively
num_items : int
u_nodes : np.int32 arrays
User indices
v_nodes : np.int32 array
item (movie) indices
ratings : np.float32 array
User/item ratings s.t. ratings[k] is the rating given by user u_nodes[k] to
item v_nodes[k]. Note that that the all pairs u_nodes[k]/v_nodes[k] are unique, but
not necessarily all u_nodes[k] or all v_nodes[k] separately.
u_features: np.float32 array, or None
If present in dataset, contains the features of the users.
v_features: np.float32 array, or None
If present in dataset, contains the features of the users.
seed: int,
For datashuffling seed with pythons own random.shuffle, as in CF-NADE.
"""
u_features = None
v_features = None
print('Loading dataset', fname)
data_dir = 'raw_data/' + fname
if fname == 'ml-100k':
# Check if files exist and download otherwise
files = ['/u.data', '/u.item', '/u.user']
download_dataset(fname, files, data_dir)
sep = '\t'
filename = data_dir + files[0]
dtypes = {
'u_nodes': np.int32, 'v_nodes': np.int32,
'ratings': np.float32, 'timestamp': np.float64}
data = pd.read_csv(
filename, sep=sep, header=None,
names=['u_nodes', 'v_nodes', 'ratings', 'timestamp'], dtype=dtypes)
# shuffle here like cf-nade paper with python's own random class
# make sure to convert to list, otherwise random.shuffle acts weird on it without a warning
data_array = data.values.tolist()
random.seed(seed)
random.shuffle(data_array)
data_array = np.array(data_array)
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'])
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)
# Movie features (genres)
sep = r'|'
movie_file = data_dir + files[1]
movie_headers = ['movie id', 'movie title', 'release date', 'video release date',
'IMDb URL', 'unknown', 'Action', 'Adventure', 'Animation',
'Childrens', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy',
'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi',
'Thriller', 'War', 'Western']
movie_df = pd.read_csv(movie_file, sep=sep, header=None,
names=movie_headers, engine='python')
genre_headers = movie_df.columns.values[6:]
num_genres = genre_headers.shape[0]
v_features = np.zeros((num_items, num_genres), dtype=np.float32)
for movie_id, g_vec in zip(movie_df['movie id'].values.tolist(), movie_df[genre_headers].values.tolist()):
# Check if movie_id was listed in ratings file and therefore in mapping dictionary
if movie_id in v_dict.keys():
v_features[v_dict[movie_id], :] = g_vec
# User features
sep = r'|'
users_file = data_dir + files[2]
users_headers = ['user id', 'age', 'gender', 'occupation', 'zip code']
users_df = pd.read_csv(users_file, sep=sep, header=None,
names=users_headers, engine='python')
occupation = set(users_df['occupation'].values.tolist())
gender_dict = {'M': 0., 'F': 1.}
occupation_dict = {f: i for i, f in enumerate(occupation, start=2)}
num_feats = 2 + len(occupation_dict)
u_features = np.zeros((num_users, num_feats), dtype=np.float32)
for _, row in users_df.iterrows():
u_id = row['user id']
if u_id in u_dict.keys():
# age
u_features[u_dict[u_id], 0] = row['age']
# gender
u_features[u_dict[u_id], 1] = gender_dict[row['gender']]
# occupation
u_features[u_dict[u_id], occupation_dict[row['occupation']]] = 1.
u_features = sp.csr_matrix(u_features)
v_features = sp.csr_matrix(v_features)
elif fname == 'ml-1m':
# Check if files exist and download otherwise
files = ['/ratings.dat', '/movies.dat', '/users.dat']
download_dataset(fname, files, data_dir)
sep = r'\:\:'
filename = data_dir + files[0]
dtypes = {
'u_nodes': np.int64, 'v_nodes': np.int64,
'ratings': np.float32, 'timestamp': np.float64}
# use engine='python' to ignore warning about switching to python backend when using regexp for sep
data = pd.read_csv(filename, sep=sep, header=None,
names=['u_nodes', 'v_nodes', 'ratings', 'timestamp'], converters=dtypes, engine='python')
# shuffle here like cf-nade paper with python's own random class
# make sure to convert to list, otherwise random.shuffle acts weird on it without a warning
data_array = data.values.tolist()
random.seed(seed)
random.shuffle(data_array)
data_array = np.array(data_array)
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'])
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.int64)
ratings = ratings.astype(np.float32)
# Load movie features
movies_file = data_dir + files[1]
movies_headers = ['movie_id', 'title', 'genre']
movies_df = pd.read_csv(movies_file, sep=sep, header=None,
names=movies_headers, engine='python')
# Extracting all genres
genres = []
for s in movies_df['genre'].values:
genres.extend(s.split('|'))
genres = list(set(genres))
num_genres = len(genres)
genres_dict = {g: idx for idx, g in enumerate(genres)}
# Creating 0 or 1 valued features for all genres
v_features = np.zeros((num_items, num_genres), dtype=np.float32)
for movie_id, s in zip(movies_df['movie_id'].values.tolist(), movies_df['genre'].values.tolist()):
# Check if movie_id was listed in ratings file and therefore in mapping dictionary
if movie_id in v_dict.keys():
gen = s.split('|')
for g in gen:
v_features[v_dict[movie_id], genres_dict[g]] = 1.
# Load user features
users_file = data_dir + files[2]
users_headers = ['user_id', 'gender', 'age', 'occupation', 'zip-code']
users_df = pd.read_csv(users_file, sep=sep, header=None,
names=users_headers, engine='python')
# Extracting all features
cols = users_df.columns.values[1:]
cntr = 0
feat_dicts = []
for header in cols:
d = dict()
feats = np.unique(users_df[header].values).tolist()
d.update({f: i for i, f in enumerate(feats, start=cntr)})
feat_dicts.append(d)
cntr += len(d)
num_feats = sum(len(d) for d in feat_dicts)
u_features = np.zeros((num_users, num_feats), dtype=np.float32)
for _, row in users_df.iterrows():
u_id = row['user_id']
if u_id in u_dict.keys():
for k, header in enumerate(cols):
u_features[u_dict[u_id], feat_dicts[k][row[header]]] = 1.
u_features = sp.csr_matrix(u_features)
v_features = sp.csr_matrix(v_features)
elif fname == 'ml-10m':
# Check if files exist and download otherwise
files = ['/ratings.dat']
download_dataset(fname, files, data_dir)
sep = r'\:\:'
filename = data_dir + files[0]
dtypes = {
'u_nodes': np.int64, 'v_nodes': np.int64,
'ratings': np.float32, 'timestamp': np.float64}
# use engine='python' to ignore warning about switching to python backend when using regexp for sep
data = pd.read_csv(filename, sep=sep, header=None,
names=['u_nodes', 'v_nodes', 'ratings', 'timestamp'], converters=dtypes, engine='python')
# shuffle here like cf-nade paper with python's own random class
# make sure to convert to list, otherwise random.shuffle acts weird on it without a warning
data_array = data.values.tolist()
random.seed(seed)
random.shuffle(data_array)
data_array = np.array(data_array)
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'])
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.int64)
ratings = ratings.astype(np.float32)
else:
raise ValueError('Dataset name not recognized: ' + fname)
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),))
return num_users, num_items, u_nodes_ratings, v_nodes_ratings, ratings, u_features, v_features
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_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])
for i in range(len(ratings)):
assert(labels[idx_nonzero[i]] == rating_dict[ratings[i]])
idx_nonzero_train = idx_nonzero[0:num_train+num_val]
idx_nonzero_test = idx_nonzero[num_train+num_val:]
pairs_nonzero_train = pairs_nonzero[0:num_train+num_val]
pairs_nonzero_test = pairs_nonzero[num_train+num_val:]
# Internally shuffle training set (before splitting off validation set)
rand_idx = list(range(len(idx_nonzero_train)))
np.random.seed(1234)
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)
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 dataset =='ml-100k':
# movie features (genres)
sep = r'|'
movie_file = 'raw_data/' + dataset + '/u.item'
movie_headers = ['movie id', 'movie title', 'release date', 'video release date',
'IMDb URL', 'unknown', 'Action', 'Adventure', 'Animation',
'Childrens', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy',
'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi',
'Thriller', 'War', 'Western']
movie_df = pd.read_csv(movie_file, sep=sep, header=None,
names=movie_headers, engine='python')
genre_headers = movie_df.columns.values[6:]
num_genres = genre_headers.shape[0]
v_features = np.zeros((num_items, num_genres), dtype=np.float32)
for movie_id, g_vec in zip(movie_df['movie id'].values.tolist(), movie_df[genre_headers].values.tolist()):
# check if movie_id was listed in ratings file and therefore in mapping dictionary
if movie_id in v_dict.keys():
v_features[v_dict[movie_id], :] = g_vec
# user features
sep = r'|'
users_file = 'raw_data/' + dataset + '/u.user'
users_headers = ['user id', 'age', 'gender', 'occupation', 'zip code']
users_df = pd.read_csv(users_file, sep=sep, header=None,
names=users_headers, engine='python')
occupation = set(users_df['occupation'].values.tolist())
age = users_df['age'].values
age_max = age.max()
gender_dict = {'M': 0., 'F': 1.}
occupation_dict = {f: i for i, f in enumerate(occupation, start=2)}
num_feats = 2 + len(occupation_dict)
u_features = np.zeros((num_users, num_feats), dtype=np.float32)
for _, row in users_df.iterrows():
u_id = row['user id']
if u_id in u_dict.keys():
# age
u_features[u_dict[u_id], 0] = row['age'] / np.float(age_max)
# gender
u_features[u_dict[u_id], 1] = gender_dict[row['gender']]
# occupation
u_features[u_dict[u_id], occupation_dict[row['occupation']]] = 1.
elif dataset == 'ml-1m':
# load movie features
movies_file = 'raw_data/' + dataset + '/movies.dat'
movies_headers = ['movie_id', 'title', 'genre']
movies_df = pd.read_csv(movies_file, sep=sep, header=None,
names=movies_headers, engine='python')
# extracting all genres
genres = []
for s in movies_df['genre'].values:
genres.extend(s.split('|'))
genres = list(set(genres))
num_genres = len(genres)
genres_dict = {g: idx for idx, g in enumerate(genres)}
# creating 0 or 1 valued features for all genres
v_features = np.zeros((num_items, num_genres), dtype=np.float32)
for movie_id, s in zip(movies_df['movie_id'].values.tolist(), movies_df['genre'].values.tolist()):
# check if movie_id was listed in ratings file and therefore in mapping dictionary
if movie_id in v_dict.keys():
gen = s.split('|')
for g in gen:
v_features[v_dict[movie_id], genres_dict[g]] = 1.
# load user features
users_file = 'raw_data/' + dataset + '/users.dat'
users_headers = ['user_id', 'gender', 'age', 'occupation', 'zip-code']
users_df = pd.read_csv(users_file, sep=sep, header=None,
names=users_headers, engine='python')
# extracting all features
cols = users_df.columns.values[1:]
cntr = 0
feat_dicts = []
for header in cols:
d = dict()
feats = np.unique(users_df[header].values).tolist()
d.update({f: i for i, f in enumerate(feats, start=cntr)})
feat_dicts.append(d)
cntr += len(d)
num_feats = sum(len(d) for d in feat_dicts)
u_features = np.zeros((num_users, num_feats), dtype=np.float32)
for _, row in users_df.iterrows():
u_id = row['user_id']
if u_id in u_dict.keys():
for k, header in enumerate(cols):
u_features[u_dict[u_id], feat_dicts[k][row[header]]] = 1.
else:
raise ValueError('Invalid dataset option %s' % dataset)
u_features = sp.csr_matrix(u_features)
v_features = sp.csr_matrix(v_features)
print("User features shape: "+str(u_features.shape))
print("Item features shape: "+str(v_features.shape))
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
if __name__ == '__main__':
dataset = MovieLens("ml-100k", testing=True)