def __init__(self, pre_splitted_path): test_percentage = 0.2 validation_percentage = 0.2 pre_splitted_path += "data_split/" pre_splitted_filename = "splitted_data_" # If directory does not exist, create if not os.path.exists(pre_splitted_path): os.makedirs(pre_splitted_path) try: print( "Dataset_MovielensHetrec2011: Attempting to load pre-splitted data" ) for attrib_name, attrib_object in load_data_dict_zip( pre_splitted_path, pre_splitted_filename).items(): self.__setattr__(attrib_name, attrib_object) except FileNotFoundError: print( "Dataset_MovielensHetrec2011: Pre-splitted data not found, building new one" ) data_reader = MovielensHetrec2011Reader_DataManager() loaded_dataset = data_reader.load_data() URM_all = loaded_dataset.get_URM_all() # keep only ratings 5 URM_all.data = URM_all.data == 5 URM_all.eliminate_zeros() # create train - test - validation URM_train_original, URM_test = split_train_validation_percentage_user_wise( URM_all, train_percentage=1 - test_percentage, verbose=False) URM_train, URM_validation = split_train_validation_percentage_user_wise( URM_train_original, train_percentage=1 - validation_percentage, verbose=False) self.URM_DICT = { "URM_train": URM_train, "URM_test": URM_test, "URM_validation": URM_validation, } save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path, pre_splitted_filename) print("Dataset_MovielensHetrec2011: Dataset loaded") ut.print_stat_datareader(self)
def __init__(self): super(Movielens100KReader, self).__init__() pre_splitted_path = "Data_manager_split_datasets/Movielens100K/KDD/MCRec_our_interface/" pre_splitted_filename = "splitted_data" original_data_path = "Conferences/KDD/MCRec_github/data/" # If directory does not exist, create if not os.path.exists(pre_splitted_path): os.makedirs(pre_splitted_path) try: print("Movielens100KReader: Attempting to load pre-splitted data") for attrib_name, attrib_object in load_data_dict(pre_splitted_path, pre_splitted_filename).items(): self.__setattr__(attrib_name, attrib_object) except FileNotFoundError: print("Movielens100KReader: Pre-splitted data not found, building new one") print("Movielens100KReader: loading URM") from Conferences.KDD.MCRec_github.code.Dataset import Dataset dataset = 'ml-100k' dataset = Dataset(original_data_path + dataset) URM_train, testRatings, testNegatives = dataset.trainMatrix, dataset.testRatings, dataset.testNegatives # Dataset adds 1 to user and item id, removing it to restore 0 indexing URM_train = sps.coo_matrix(URM_train) URM_train.row -= 1 URM_train.col -= 1 self.URM_train = sps.csr_matrix((np.ones_like(URM_train.data), (URM_train.row, URM_train.col))) num_users, num_items = self.URM_train.shape # Build sparse matrices from lists URM_test_builder = IncrementalSparseMatrix(n_rows=num_users, n_cols=num_items) URM_test_negative_builder = IncrementalSparseMatrix(n_rows=num_users, n_cols=num_items) for user_index in range(len(testRatings)): user_id = testRatings[user_index][0] current_user_test_items = testRatings[user_index][1:] current_user_test_negative_items = testNegatives[user_index] current_user_test_items = np.array(current_user_test_items) -1 current_user_test_negative_items = np.array(current_user_test_negative_items) -1 URM_test_builder.add_single_row(user_id -1, current_user_test_items, 1.0) URM_test_negative_builder.add_single_row(user_id -1, current_user_test_negative_items, 1.0) # the test data has repeated data, apparently self.URM_test = URM_test_builder.get_SparseMatrix() self.URM_test_negative = URM_test_negative_builder.get_SparseMatrix() # Split validation from train as 10% from Data_manager.split_functions.split_train_validation import split_train_validation_percentage_user_wise self.URM_train, self.URM_validation = split_train_validation_percentage_user_wise(self.URM_train, train_percentage=0.9) # Load features data_reader = Movielens100KReader_DataManager() data_reader.load_data() zipFile_path = data_reader.DATASET_SPLIT_ROOT_FOLDER + data_reader.DATASET_SUBFOLDER dataFile = zipfile.ZipFile(zipFile_path + "ml-100k.zip") ICM_path = dataFile.extract("ml-100k/u.item", path=zipFile_path + "decompressed/") ICM_genre = self._loadICM(ICM_path) ICM_genre = ICM_genre.get_SparseMatrix() shutil.rmtree(zipFile_path + "decompressed", ignore_errors=True) self.ICM_dict = {"ICM_genre": ICM_genre} data_dict = { "URM_train": self.URM_train, "URM_test": self.URM_test, "URM_validation": self.URM_validation, "URM_test_negative": self.URM_test_negative, "ICM_dict": self.ICM_dict, } save_data_dict(data_dict, pre_splitted_path, pre_splitted_filename) print("Movielens100KReader: loading complete")
def __init__(self): test_percentage = 0.2 validation_percentage = 0.2 pre_splitted_path = "Data_manager_split_datasets/AmazonInstantVideo/RecSys/SpectralCF_our_interface/" pre_splitted_filename = "splitted_data" ratings_file_name = "ratings_Amazon_Instant_Video.csv" # If directory does not exist, create if not os.path.exists(pre_splitted_path): os.makedirs(pre_splitted_path) try: print( "Dataset_AmazonInstantVideo: Attempting to load pre-splitted data" ) for attrib_name, attrib_object in load_data_dict( pre_splitted_path, pre_splitted_filename).items(): self.__setattr__(attrib_name, attrib_object) except FileNotFoundError: print( "Dataset_AmazonInstantVideo: Pre-splitted data not found, building new one" ) folder_path = self.DATASET_SPLIT_ROOT_FOLDER + self.DATASET_SUBFOLDER downloadFromURL(self.DATASET_URL, folder_path, ratings_file_name) # read Amazon Instant Video df = pd.read_csv(folder_path + ratings_file_name, sep=',', header=None, names=['user', 'item', 'rating', 'timestamp'])[['user', 'item', 'rating']] # keep only ratings = 5 URM_train_builder = IncrementalSparseMatrix( auto_create_col_mapper=True, auto_create_row_mapper=True) URM_train_builder.add_data_lists(df['user'].values, df['item'].values, df['rating'].values) URM_all = URM_train_builder.get_SparseMatrix() URM_all.data = URM_all.data == 5 URM_all.eliminate_zeros() # keep only users with at least 5 ratings URM_all = ut.filter_urm(URM_all, user_min_number_ratings=5, item_min_number_ratings=1) # create train - test - validation URM_train_original, self.URM_test = split_train_validation_percentage_user_wise( URM_all, train_percentage=1 - test_percentage, verbose=False) self.URM_train, self.URM_validation = split_train_validation_percentage_user_wise( URM_train_original, train_percentage=1 - validation_percentage, verbose=False) data_dict = { "URM_train": self.URM_train, "URM_test": self.URM_test, "URM_validation": self.URM_validation, } save_data_dict(data_dict, pre_splitted_path, pre_splitted_filename) print("Dataset_AmazonInstantVideo: Dataset loaded") ut.print_stat_datareader(self)
def __init__(self, pre_splitted_path, type="original", cold_start=False, cold_items=None): assert type in ["original", "ours"] pre_splitted_path += "data_split/" pre_splitted_filename = "splitted_data_" # their mode in cold start mode = 1 # path for pre existed movielens1M split movielens_splitted_path = "Conferences/RecSys/SpectralCF_github/data/ml-1m/" # If directory does not exist, create if not os.path.exists(pre_splitted_path): os.makedirs(pre_splitted_path) try: print("Dataset_Movielens1M: Attempting to load pre-splitted data") for attrib_name, attrib_object in load_data_dict_zip( pre_splitted_path, pre_splitted_filename).items(): self.__setattr__(attrib_name, attrib_object) except FileNotFoundError: print( "Dataset_Movielens1M: Pre-splitted data not found, building new one" ) if type == "original": assert (cold_start is False) # use the SpectralCF class to read data data_generator = Data( train_file=movielens_splitted_path + 'train_users.dat', test_file=movielens_splitted_path + 'test_users.dat', batch_size=BATCH_SIZE) # convert train into csr full_train_matrix = sps.csr_matrix(data_generator.R) URM_train_original = full_train_matrix # convert test into csr test_set = data_generator.test_set uids, items = [], [] for uid in test_set.keys(): uids += np.full(len(test_set[uid]), uid).tolist() items += test_set[uid] test_matrix = sps.csr_matrix( (np.ones(len(items)), (uids, items)), shape=(full_train_matrix.shape)) if not cold_start: URM_test = test_matrix # create validation URM_train, URM_validation = split_train_validation_percentage_user_wise( URM_train_original, train_percentage=0.9, verbose=False) else: print('nothing') elif type == "ours": data_reader = Movielens1MReader_DataManager() loaded_dataset = data_reader.load_data() URM_all = loaded_dataset.get_URM_all() URM_all.data = URM_all.data == 5 URM_all.eliminate_zeros() if not cold_start: URM_train, URM_test = split_train_validation_percentage_user_wise( URM_all, train_percentage=0.8, verbose=False) URM_train, URM_validation = split_train_validation_percentage_user_wise( URM_train, train_percentage=0.9, verbose=False) else: if mode == 1: # their mode, cold start for full dataset URM_train, URM_test = split_train_validation_cold_start_user_wise( URM_all, full_train_percentage=0.0, cold_items=cold_items, verbose=False) URM_test, URM_validation = split_train_validation_percentage_user_wise( URM_test, train_percentage=0.9, verbose=False) if mode == 2: # cold start only for some users URM_train, URM_test = split_train_validation_cold_start_user_wise( URM_all, full_train_percentage=0.8, cold_items=cold_items, verbose=False) URM_train, URM_validation = split_train_validation_cold_start_user_wise( URM_train, full_train_percentage=0.9, cold_items=cold_items, verbose=False) self.URM_DICT = { "URM_train": URM_train, "URM_test": URM_test, "URM_validation": URM_validation, } save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path, pre_splitted_filename) print("Dataset_Movielens1M: Dataset loaded") ut.print_stat_datareader(self)