コード例 #1
0
    def __init__(self, path):
        '''
        Constructor
        '''
        trainMatrix = self.load_rating_file_as_matrix(path + ".train.rating")
        testRatings = self.load_rating_file_as_matrix(path + ".test.rating")
        testNegatives = self.load_negative_file(path + ".test.negative")
        assert len(testRatings) == len(testNegatives)

        self.num_users, self.num_items = trainMatrix.shape

        from Base.Recommender_utils import reshapeSparse

        self.URM_train = trainMatrix.tocsr()
        self.URM_test = testRatings.tocsr()

        shape = (max(self.URM_train.shape[0], self.URM_test.shape[0]),
                 max(self.URM_train.shape[1], self.URM_test.shape[1]))

        self.URM_train = reshapeSparse(self.URM_train, shape)
        self.URM_test = reshapeSparse(self.URM_test, shape)

        URM_test_negatives_builder = IncrementalSparseMatrix(n_rows=shape[0],
                                                             n_cols=shape[1])

        for user_index in range(len(testNegatives)):
            user_test_items = testNegatives[user_index]

            URM_test_negatives_builder.add_single_row(user_index,
                                                      user_test_items,
                                                      data=1.0)

        self.URM_test_negative = URM_test_negatives_builder.get_SparseMatrix()
コード例 #2
0
    def _load_data_file(self, filePath, separator=" "):

        URM_builder = IncrementalSparseMatrix(auto_create_row_mapper=False,
                                              auto_create_col_mapper=False)

        fileHandle = open(filePath, "r")
        user_index = 0

        for line in fileHandle:

            if (user_index % 1000000 == 0):
                print("Processed {} cells".format(user_index))

            if (len(line)) > 1:

                line = line.replace("\n", "")
                line = line.split(separator)

                if len(line) > 0:

                    if line[0] != "0":

                        line = [int(line[i]) for i in range(len(line))]

                        URM_builder.add_single_row(user_index,
                                                   line[1:],
                                                   data=1.0)

            user_index += 1

        fileHandle.close()

        return URM_builder
コード例 #3
0
    def test_IncrementalSparseMatrix_add_rows(self):

        import numpy as np

        n_rows = 100
        n_cols = 200

        randomMatrix = sps.random(n_rows, n_cols, density=0.01, format="csr")

        incrementalMatrix = IncrementalSparseMatrix(n_rows=n_rows,
                                                    n_cols=n_cols)

        for row in range(n_rows):

            row_data = randomMatrix.indices[randomMatrix.
                                            indptr[row]:randomMatrix.indptr[row
                                                                            +
                                                                            1]]

            incrementalMatrix.add_single_row(row, row_data, 5.0)

        randomMatrix.data = np.ones_like(randomMatrix.data) * 5.0

        randomMatrix_incremental = incrementalMatrix.get_SparseMatrix()

        assert sparse_are_equals(randomMatrix, randomMatrix_incremental)
コード例 #4
0
def split_train_validation_test_negative_leave_one_out_user_wise(URM_all, negative_items_per_positive=50, verbose=True,
                                                                 at_least_n_train_items_test=0,
                                                                 at_least_n_train_items_validation=0):
    """
    This function creates a Train, Test, Validation split with negative items sampled
    The split is perfomed user-wise, hold 1 out for validation and test
    :param URM_all:
    :param negative_items_per_positive:
    :return:
    """

    URM_all = sps.csr_matrix(URM_all)

    n_rows, n_cols = URM_all.shape

    print('Creation test...')
    URM_train_all, URM_test = split_train_validation_leave_one_out_user_wise(URM_all,
                                                                             at_least_n_train_items=at_least_n_train_items_test,
                                                                             verbose=verbose)

    print('Creation validation...')
    URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(URM_train_all,
                                                                               at_least_n_train_items=at_least_n_train_items_validation,
                                                                               verbose=verbose)

    URM_negative_builder = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)

    all_items = np.arange(0, n_cols, dtype=np.int)

    for user_index in range(URM_train_all.shape[0]):

        if user_index % 10000 == 0:
            print("split_data_train_validation_test_negative: user {} of {}".format(user_index, URM_all.shape[0]))

        start_pos = URM_all.indptr[user_index]
        end_pos = URM_all.indptr[user_index + 1]

        user_profile = URM_all.indices[start_pos:end_pos]

        unobserved_index = np.in1d(all_items, user_profile, assume_unique=True, invert=True)

        unobserved_items = all_items[unobserved_index]
        np.random.shuffle(unobserved_items)

        n_test_items = URM_test.indptr[user_index + 1] - URM_test.indptr[user_index]

        num_negative_items = n_test_items * negative_items_per_positive

        if num_negative_items > len(unobserved_items):
            print(
                "split_data_train_validation_test_negative: WARNING number of negative to sample for user {} is greater than available negative items {}".format(
                    num_negative_items, len(unobserved_items)))
            num_negative_items = min(num_negative_items, len(unobserved_items))

        URM_negative_builder.add_single_row(user_index, unobserved_items[:num_negative_items], 1.0)

    URM_negative = URM_negative_builder.get_SparseMatrix()

    return URM_train, URM_validation, URM_test, URM_negative
コード例 #5
0
def split_data_train_validation_test_negative_user_wise(URM_all, negative_items_per_positive=50):
    """
    This function creates a Train, Test, Validation split with negative items sampled
    The split is perfomed user-wise, 20% is test, 80% is train. Train is further divided in 90% final train and 10% validation
    :param URM_all:
    :param negative_items_per_positive:
    :return:
    """

    URM_all = sps.csr_matrix(URM_all)

    n_rows, n_cols = URM_all.shape

    URM_train_all, URM_test = split_train_validation_percentage_user_wise(URM_all, train_percentage=0.8)

    URM_train, URM_validation = split_train_validation_percentage_user_wise(URM_train_all, train_percentage=0.9)

    URM_negative_builder = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)

    all_items = np.arange(0, n_cols, dtype=np.int)

    for user_index in range(URM_train_all.shape[0]):

        if user_index % 10000 == 0:
            print("split_data_train_validation_test_negative: user {} of {}".format(user_index, URM_all.shape[0]))

        start_pos = URM_all.indptr[user_index]
        end_pos = URM_all.indptr[user_index + 1]

        user_profile = URM_all.indices[start_pos:end_pos]

        unobserved_index = np.in1d(all_items, user_profile, assume_unique=True, invert=True)

        unobserved_items = all_items[unobserved_index]
        np.random.shuffle(unobserved_items)

        n_test_items = URM_test.indptr[user_index + 1] - URM_test.indptr[user_index]

        num_negative_items = n_test_items * negative_items_per_positive

        if num_negative_items > len(unobserved_items):
            print(
                "split_data_train_validation_test_negative: WARNING number of negative to sample for user {} is greater than available negative items {}".format(
                    num_negative_items, len(unobserved_items)))
            num_negative_items = min(num_negative_items, len(unobserved_items))

        URM_negative_builder.add_single_row(user_index, unobserved_items[:num_negative_items], 1.0)

    URM_negative = URM_negative_builder.get_SparseMatrix()

    return URM_train, URM_validation, URM_test, URM_negative
コード例 #6
0
    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, pre_splitted_path):

        super(Movielens1MReader, self).__init__()

        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_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"
            )

            # Ensure file is loaded as matrix
            Dataset_github.load_rating_file_as_list = Dataset_github.load_rating_file_as_matrix

            dataset = Dataset_github("Conferences/WWW/NeuMF_github/Data/ml-1m")

            URM_train_original, URM_test = dataset.trainMatrix, dataset.testRatings

            URM_train_original = URM_train_original.tocsr()
            URM_test = URM_test.tocsr()

            from Base.Recommender_utils import reshapeSparse

            shape = (max(URM_train_original.shape[0], URM_test.shape[0]),
                     max(URM_train_original.shape[1], URM_test.shape[1]))

            URM_train_original = reshapeSparse(URM_train_original, shape)
            URM_test = reshapeSparse(URM_test, shape)

            URM_test_negatives_builder = IncrementalSparseMatrix(
                n_rows=shape[0], n_cols=shape[1])

            for user_index in range(len(dataset.testNegatives)):

                user_test_items = dataset.testNegatives[user_index]

                URM_test_negatives_builder.add_single_row(user_index,
                                                          user_test_items,
                                                          data=1.0)

            URM_test_negative = URM_test_negatives_builder.get_SparseMatrix()

            URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                URM_train_original.copy())

            self.URM_DICT = {
                "URM_train_original": URM_train_original,
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_test_negative": URM_test_negative,
                "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")
コード例 #8
0
    def __init__(self, pre_splitted_path, original=True):

        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_{}: Attempting to load pre-splitted data".format(
                self.DATASET_NAME))

            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_{}: Pre-splitted data not found, building new one".
                  format(self.DATASET_NAME))

            compressed_file_folder = "Conferences/IJCAI/ConvNCF_github/Data/"
            decompressed_file_folder = "Data_manager_split_datasets/Yelp/"

            # compressed_file = tarfile.open(compressed_file_folder + "yelp.test.negative.gz", "r:gz")
            # compressed_file.extract("yelp.test.negative", path=decompressed_file_folder + "decompressed/")
            # compressed_file.close()
            #
            # compressed_file = tarfile.open(compressed_file_folder + "yelp.test.rating.gz", "r:gz")
            # compressed_file.extract("yelp.test.rating", path=decompressed_file_folder + "decompressed/")
            # compressed_file.close()
            #
            # compressed_file = tarfile.open(compressed_file_folder + "yelp.train.rating.gz", "r:gz")
            # compressed_file.extract("yelp.train.rating", path=decompressed_file_folder + "decompressed/")
            # compressed_file.close()

            # if original:

            Dataset_github.load_rating_file_as_list = Dataset_github.load_training_file_as_matrix

            try:
                dataset = Dataset_github(compressed_file_folder + "yelp")

            except FileNotFoundError as exc:

                print(
                    "Dataset_{}: Uncompressed files not found, please manually decompress the *.gz files in this folder: '{}'"
                    .format(self.DATASET_NAME, compressed_file_folder))

                raise exc

            URM_train_original, URM_test = dataset.trainMatrix, dataset.testRatings

            n_users = max(URM_train_original.shape[0], URM_test.shape[0])
            n_items = max(URM_train_original.shape[1], URM_test.shape[1])

            URM_train_original = sps.csr_matrix(URM_train_original,
                                                shape=(n_users, n_items))
            URM_test = sps.csr_matrix(URM_test, shape=(n_users, n_items))

            URM_train_original.data = np.ones_like(URM_train_original.data)
            URM_test.data = np.ones_like(URM_test.data)

            URM_test_negatives_builder = IncrementalSparseMatrix(
                n_rows=n_users, n_cols=n_items)

            n_negative_samples = 999
            for user_index in range(len(dataset.testNegatives)):
                user_test_items = dataset.testNegatives[user_index]
                if len(user_test_items) != n_negative_samples:
                    print(
                        "user id: {} has {} negative items instead {}".format(
                            user_index, len(user_test_items),
                            n_negative_samples))
                URM_test_negatives_builder.add_single_row(user_index,
                                                          user_test_items,
                                                          data=1.0)

            URM_test_negative = URM_test_negatives_builder.get_SparseMatrix()
            URM_test_negative.data = np.ones_like(URM_test_negative.data)

            URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                URM_train_original.copy(), verbose=False)

            #
            # else:
            #     data_reader = YelpReader_DataManager()
            #     loaded_dataset = data_reader.load_data()
            #
            #     URM_all = loaded_dataset.get_URM_all()
            #
            #     URM_timestamp = URM_all.copy()
            #
            #     URM_all.data = np.ones_like(URM_all.data)
            #
            #     URM_train, URM_validation, URM_test, URM_negative = split_data_on_timestamp(URM_all, URM_timestamp, negative_items_per_positive=999)
            #     URM_train = URM_train + URM_validation
            #     URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(URM_train, verbose=False)

            shutil.rmtree(decompressed_file_folder + "decompressed/",
                          ignore_errors=True)

            self.URM_DICT = {
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_validation": URM_validation,
                "URM_test_negative": URM_test_negative,
            }

            save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path,
                               pre_splitted_filename)

        print("{}: Dataset loaded".format(self.DATASET_NAME))

        ut.print_stat_datareader(self)
コード例 #9
0
    def __init__(self, pre_splitted_path, type='original'):
        assert type in ["original", "ours"]

        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_{}: Attempting to load pre-splitted data".format(
                self.DATASET_NAME))

            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_{}: Pre-splitted data not found, building new one".
                  format(self.DATASET_NAME))

            from Conferences.IJCAI.CoupledCF_original import LoadMovieDataCnn as DatareaderOriginal
            path = "Conferences/IJCAI/CoupledCF_original/ml-1m/"

            n_users, gender, age, occupation = DatareaderOriginal.load_user_attributes(
                path=path, split=True)
            n_items, items_genres_mat = DatareaderOriginal.load_itemGenres_as_matrix(
                path=path)
            ratings = DatareaderOriginal.load_rating_train_as_matrix(path=path)
            testRatings = DatareaderOriginal.load_rating_file_as_list(
                path=path)
            testNegatives = DatareaderOriginal.load_negative_file(path=path)

            URM_all = ratings.tocsr()

            UCM_gender = gender.tocsr()
            UCM_age = age.tocsr()
            UCM_occupation = occupation.tocsr()
            UCM_all = sps.hstack((UCM_gender, UCM_age, UCM_occupation)).tocsr()

            ICM_all = sps.csr_matrix(items_genres_mat)

            testRatings = np.array(testRatings).T
            URM_test_builder = IncrementalSparseMatrix(n_rows=n_users + 1,
                                                       n_cols=n_items + 1)
            URM_test_builder.add_data_lists(testRatings[0], testRatings[1],
                                            np.ones(len(testRatings[0])))

            URM_test = URM_test_builder.get_SparseMatrix()

            URM_test_negatives_builder = IncrementalSparseMatrix(
                n_rows=n_users + 1, n_cols=n_items + 1)

            # care here, the test negative start from index 0 but it refer to user index 1 (user index start from 1)
            n_negative_samples = 99
            for index in range(len(testNegatives)):
                user_test_items = testNegatives[index]
                if len(user_test_items) != n_negative_samples:
                    print(
                        "user id: {} has {} negative items instead {}".format(
                            index + 1, len(user_test_items),
                            n_negative_samples))
                URM_test_negatives_builder.add_single_row(index + 1,
                                                          user_test_items,
                                                          data=1.0)

            URM_test_negative = URM_test_negatives_builder.get_SparseMatrix()
            URM_test_negative.data = np.ones_like(URM_test_negative.data)

            if type == 'original':
                URM_test = URM_test
                URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                    URM_all.copy(), verbose=False)

            else:  # redo the split
                URM_full = URM_all + URM_test
                URM_temp, URM_test = split_train_validation_leave_one_out_user_wise(
                    URM_full.copy(), verbose=False)
                URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                    URM_temp.copy(), verbose=False)

            self.ICM_DICT = {
                "UCM_gender": UCM_gender,
                "UCM_occupation": UCM_occupation,
                "UCM_age": UCM_age,
                "UCM_all": UCM_all,
                "ICM_all": ICM_all,
            }

            self.URM_DICT = {
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_validation": URM_validation,
                "URM_test_negative": URM_test_negative,
            }

            save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path,
                               pre_splitted_filename)

        print("{}: Dataset loaded".format(self.DATASET_NAME))

        ut.print_stat_datareader(self)
def split_data_on_timestamp(URM_all, URM_timestamp, negative_items_per_positive=100):

    URM_all = sps.csr_matrix(URM_all)
    URM_timestamp = sps.csr_matrix(URM_timestamp)

    n_rows, n_cols = URM_all.shape


    URM_train_builder = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)
    URM_test_builder = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)
    URM_validation_builder = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)
    URM_negative_builder = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)

    all_items = np.arange(0, n_cols, dtype=np.int)


    for user_index in range(URM_all.shape[0]):

        if user_index % 10000 == 0:
            print("split_data_on_sequence: user {} of {}".format(user_index, URM_all.shape[0]))

        start_pos = URM_all.indptr[user_index]
        end_pos = URM_all.indptr[user_index+1]

        user_profile = URM_all.indices[start_pos:end_pos]
        user_data = URM_all.data[start_pos:end_pos]
        user_sequence = URM_timestamp.data[start_pos:end_pos]


        unobserved_index = np.in1d(all_items, user_profile, assume_unique=True, invert=True)

        unobserved_items = all_items[unobserved_index]
        np.random.shuffle(unobserved_items)

        URM_negative_builder.add_single_row(user_index, unobserved_items[:negative_items_per_positive], 1.0)


        if len(user_profile) >= 3:



            # Test contain the first one, validation the second
            min_pos = np.argmax(user_sequence)

            venue_index = user_profile[min_pos]
            venue_data = user_data[min_pos]

            URM_test_builder.add_data_lists([user_index], [venue_index], [venue_data])

            user_profile = np.delete(user_profile, min_pos)
            user_data = np.delete(user_data, min_pos)
            user_sequence = np.delete(user_sequence, min_pos)


            min_pos = np.argmax(user_sequence)

            venue_index = user_profile[min_pos]
            venue_data = user_data[min_pos]

            URM_validation_builder.add_data_lists([user_index], [venue_index], [venue_data])

            user_profile = np.delete(user_profile, min_pos)
            user_data = np.delete(user_data, min_pos)
            #user_sequence = np.delete(user_sequence, min_pos)


            URM_train_builder.add_data_lists([user_index]*len(user_profile), user_profile, user_data)


    URM_train = URM_train_builder.get_SparseMatrix()
    URM_validation = URM_validation_builder.get_SparseMatrix()
    URM_test = URM_test_builder.get_SparseMatrix()
    URM_negative = URM_negative_builder.get_SparseMatrix()



    return URM_train, URM_validation, URM_test, URM_negative
コード例 #11
0
    def __init__(self, pre_splitted_path, type="original"):

        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_{}: Attempting to load pre-splitted data".format(
                self.DATASET_NAME))

            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_{}: Pre-splitted data not found, building new one".
                  format(self.DATASET_NAME))

            if type == "original":

                # Ensure file is loaded as matrix
                Dataset_github.load_rating_file_as_list = Dataset_github.load_rating_file_as_matrix

                dataset = Dataset_github(
                    "Conferences/IJCAI/DELF_original/Data/ml-1m")

                URM_train, URM_validation, URM_test, testNegatives = dataset.trainMatrix, dataset.validRatings, \
                                                                     dataset.testRatings, dataset.testNegatives

                URM_train = URM_train.tocsr()
                URM_validation = URM_validation.tocsr()
                URM_test = URM_test.tocsr()
                URM_timestamp = "no"

                from Base.Recommender_utils import reshapeSparse

                shape = (max(URM_train.shape[0], URM_validation.shape[0],
                             URM_test.shape[0]),
                         max(URM_train.shape[1], URM_validation.shape[1],
                             URM_test.shape[1]))

                URM_train = reshapeSparse(URM_train, shape)
                URM_validation = reshapeSparse(URM_validation, shape)
                URM_test = reshapeSparse(URM_test, shape)

                URM_test_negatives_builder = IncrementalSparseMatrix(
                    n_rows=shape[0], n_cols=shape[1])

                for user_index in range(len(dataset.testNegatives)):

                    user_test_items = dataset.testNegatives[user_index]

                    URM_test_negatives_builder.add_single_row(user_index,
                                                              user_test_items,
                                                              data=1.0)

                URM_test_negative = URM_test_negatives_builder.get_SparseMatrix(
                )

            elif type == "ours":

                # create from full dataset with leave out one time wise from ORIGINAL full dateset
                data_reader = Movielens1MReader_DataManager()
                loaded_dataset = data_reader.load_data()

                URM_all = loaded_dataset.get_URM_from_name("URM_all")
                URM_timestamp = loaded_dataset.get_URM_from_name(
                    "URM_timestamp")

                # make rating implicit
                URM_all.data = np.ones_like(URM_all.data)

                URM_train, URM_validation, URM_test, URM_test_negative = split_data_on_timestamp(
                    URM_all, URM_timestamp, negative_items_per_positive=99)

            else:
                assert False

            self.URM_DICT = {
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_validation": URM_validation,
                "URM_test_negative": URM_test_negative,
                "URM_timestamp": URM_timestamp,
            }

            save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path,
                               pre_splitted_filename)

        print("{}: Dataset loaded".format(self.DATASET_NAME))

        print_stat_datareader(self)
コード例 #12
0
    def __init__(self, pre_splitted_path, type='original'):
        assert type in ["original", "ours"]

        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_{}: Attempting to load pre-splitted data".format(
                self.DATASET_NAME))

            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_{}: Pre-splitted data not found, building new one".
                  format(self.DATASET_NAME))

            from Conferences.IJCAI.CoupledCF_original import LoadTafengDataCnn as DatareaderOriginal
            path = "Conferences/IJCAI/CoupledCF_original/tafeng/"

            n_users, user_attributes_mat = DatareaderOriginal.load_user_attributes(
                path=path)
            n_items, items_genres_mat = DatareaderOriginal.load_itemGenres_as_matrix(
                path=path)
            ratings = DatareaderOriginal.load_rating_train_as_matrix(path=path)
            testRatings = DatareaderOriginal.load_rating_file_as_list(
                path=path)
            testNegatives = DatareaderOriginal.load_negative_file(path=path)

            URM_all = ratings.tocsr()

            UCM_all = sps.csc_matrix(user_attributes_mat)
            UCM_age = UCM_all[:, 0:11].tocsr()
            UCM_region = UCM_all[:, 11:19].tocsr()
            UCM_all = UCM_all.tocsr()

            # col: 0->category, 2->asset(0-1), 1->price(0-1)
            ICM_original = sps.csc_matrix(items_genres_mat)

            # category could be used as matrix, not single row
            ICM_sub_class = ICM_original[:, 0:1].tocsr()
            max = ICM_sub_class.shape[0]
            rows, cols, data = [], [], []
            for idx in range(max):
                # we have only index 0 as col
                data_vect = ICM_sub_class.data[
                    ICM_sub_class.indptr[idx]:ICM_sub_class.indptr[idx + 1]]
                if len(data_vect) == 0:
                    # handle category value 0 that in a csr matrix is not present
                    cols.append(int(0))
                else:
                    cols.append(int(data_vect[0]))
                rows.append(idx)
                data.append(1.0)

            ICM_sub_class = sps.csr_matrix((data, (rows, cols)))
            ICM_asset = ICM_original[:, 1:2].tocsr()
            ICM_price = ICM_original[:, 2:3].tocsr()

            ICM_original = ICM_original.tocsc()
            ICM_all = sps.hstack((ICM_sub_class, ICM_asset, ICM_price))

            testRatings = np.array(testRatings).T
            URM_test_builder = IncrementalSparseMatrix(n_rows=n_users + 1,
                                                       n_cols=n_items + 1)
            URM_test_builder.add_data_lists(testRatings[0], testRatings[1],
                                            np.ones(len(testRatings[0])))
            URM_test = URM_test_builder.get_SparseMatrix()

            URM_test_negatives_builder = IncrementalSparseMatrix(
                n_rows=n_users + 1, n_cols=n_items + 1)

            # care here, the test negative start from index 0 but it refer to user index 1 (user index start from 1)
            n_negative_samples = 99
            for index in range(len(testNegatives)):
                user_test_items = testNegatives[index]
                if len(user_test_items) != n_negative_samples:
                    print(
                        "user id: {} has {} negative items instead {}".format(
                            index + 1, len(user_test_items),
                            n_negative_samples))
                URM_test_negatives_builder.add_single_row(index + 1,
                                                          user_test_items,
                                                          data=1.0)

            URM_test_negative = URM_test_negatives_builder.get_SparseMatrix()
            URM_test_negative.data = np.ones_like(URM_test_negative.data)

            if type == 'original':
                URM_test = URM_test
                URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                    URM_all.copy(), verbose=False)
            else:  # redo the split
                URM_full = URM_all + URM_test
                URM_temp, URM_test = split_train_validation_leave_one_out_user_wise(
                    URM_full.copy(), verbose=False)
                URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                    URM_temp.copy(), verbose=False)

            self.ICM_DICT = {
                "UCM_age": UCM_age,
                "UCM_region": UCM_region,
                "UCM_all": UCM_all,
                "ICM_all": ICM_all,
                "ICM_original": ICM_original,
                "ICM_sub_class": ICM_sub_class,
                "ICM_asset": ICM_asset,
                "ICM_price": ICM_price,
            }

            self.URM_DICT = {
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_validation": URM_validation,
                "URM_test_negative": URM_test_negative,
            }

            save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path,
                               pre_splitted_filename)

        print("{}: Dataset loaded".format(self.DATASET_NAME))

        ut.print_stat_datareader(self)
コード例 #13
0
    def __init__(self):

        super(PinterestICCVReader, self).__init__()

        pre_splitted_path = "Data_manager_split_datasets/PinterestICCV/WWW/NeuMF_our_interface/"

        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_Pinterest: 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_Pinterest: Pre-splitted data not found, building new one"
            )

            # Ensure file is loaded as matrix
            Dataset_github.load_rating_file_as_list = Dataset_github.load_rating_file_as_matrix

            dataset = Dataset_github(
                "Conferences/WWW/NeuMF_github/Data/pinterest-20")

            self.URM_train_original, self.URM_test = dataset.trainMatrix, dataset.testRatings

            self.URM_train_original = self.URM_train_original.tocsr()
            self.URM_test = self.URM_test.tocsr()

            from Base.Recommender_utils import reshapeSparse

            shape = (max(self.URM_train_original.shape[0],
                         self.URM_test.shape[0]),
                     max(self.URM_train_original.shape[1],
                         self.URM_test.shape[1]))

            self.URM_train_original = reshapeSparse(self.URM_train_original,
                                                    shape)
            self.URM_test = reshapeSparse(self.URM_test, shape)

            URM_test_negatives_builder = IncrementalSparseMatrix(
                n_rows=shape[0], n_cols=shape[1])

            for user_index in range(len(dataset.testNegatives)):

                user_test_items = dataset.testNegatives[user_index]

                URM_test_negatives_builder.add_single_row(user_index,
                                                          user_test_items,
                                                          data=1.0)

            self.URM_test_negative = URM_test_negatives_builder.get_SparseMatrix(
            )

            self.URM_train, self.URM_validation = split_train_validation_leave_one_out_user_wise(
                self.URM_train_original.copy())

            data_dict = {
                "URM_train_original": self.URM_train_original,
                "URM_train": self.URM_train,
                "URM_test": self.URM_test,
                "URM_test_negative": self.URM_test_negative,
                "URM_validation": self.URM_validation,
            }

            save_data_dict(data_dict, pre_splitted_path, pre_splitted_filename)

        print("Dataset_Pinterest: Dataset loaded")

        print("N_items {}, n_users {}".format(self.URM_train.shape[1],
                                              self.URM_train.shape[0]))
コード例 #14
0
    def __init__(self, pre_splitted_path):

        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_{}: Attempting to load pre-splitted data".format(
                self.DATASET_NAME))

            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_{}: Pre-splitted data not found, building new one".
                  format(self.DATASET_NAME))

            compressed_file_folder = "Conferences/IJCAI/ConvNCF_github/Data/"
            decompressed_file_folder = "Data_manager_split_datasets/Gowalla/"

            # compressed_file = tarfile.open(compressed_file_folder + "gowalla.test.negative.gz", "r:gz")
            # compressed_file.extract("yelp.test.negative", path=decompressed_file_folder + "decompressed/")
            # compressed_file.close()
            #
            # compressed_file = tarfile.open(compressed_file_folder + "gowalla.test.rating.gz", "r:gz")
            # compressed_file.extract("yelp.test.rating", path=decompressed_file_folder + "decompressed/")
            # compressed_file.close()
            #
            # compressed_file = tarfile.open(compressed_file_folder + "gowalla.train.rating.gz", "r:gz")
            # compressed_file.extract("yelp.train.rating", path=decompressed_file_folder + "decompressed/")
            # compressed_file.close()

            # if original:

            Dataset_github.load_rating_file_as_list = Dataset_github.load_training_file_as_matrix

            try:
                dataset = Dataset_github(compressed_file_folder + "gowalla")

            except FileNotFoundError as exc:

                print(
                    "Dataset_{}: Gowalla files not found, please download them and put them in this folder '{}', url: {}"
                    .format(self.DATASET_NAME, compressed_file_folder,
                            self.DATASET_URL))
                print(
                    "Dataset_{}: Uncompressed files not found, please manually decompress the *.gz files in this folder: '{}'"
                    .format(self.DATASET_NAME, compressed_file_folder))

                raise exc

            URM_train_original, URM_test = dataset.trainMatrix, dataset.testRatings

            n_users = max(URM_train_original.shape[0], URM_test.shape[0])
            n_items = max(URM_train_original.shape[1], URM_test.shape[1])

            URM_train_original = sps.csr_matrix(URM_train_original,
                                                shape=(n_users, n_items))
            URM_test = sps.csr_matrix(URM_test, shape=(n_users, n_items))

            URM_train_original.data = np.ones_like(URM_train_original.data)
            URM_test.data = np.ones_like(URM_test.data)

            URM_test_negatives_builder = IncrementalSparseMatrix(
                n_rows=n_users, n_cols=n_items)

            n_negative_samples = 999
            for user_index in range(len(dataset.testNegatives)):
                user_test_items = dataset.testNegatives[user_index]
                if len(user_test_items) != n_negative_samples:
                    print(
                        "user id: {} has {} negative items instead {}".format(
                            user_index, len(user_test_items),
                            n_negative_samples))
                URM_test_negatives_builder.add_single_row(user_index,
                                                          user_test_items,
                                                          data=1.0)

            URM_test_negative = URM_test_negatives_builder.get_SparseMatrix(
            ).tocsr()
            URM_test_negative.data = np.ones_like(URM_test_negative.data)

            URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(
                URM_train_original.copy(), verbose=False)

            #
            #
            # # NOT USED
            # # elif not time_split: #create from full dataset with random leave one out from LINKED dateset in the article since timestamp is not present.
            # #
            # #     data_reader = GowallaGithubReader_DataManager()
            # #     loaded_dataset = data_reader.load_data()
            # #
            # #     URM_all = loaded_dataset.get_URM_all()
            # #
            # #     URM_all.eliminate_zeros()
            # #
            # #     URM_all.data = np.ones_like(URM_all.data)
            # #
            # #     #use this function 2 time because the order could change slightly the number of final interactions
            # #     #with this order we get the same number of interactions as in the paper
            # #     URM_all = filter_urm(URM_all, user_min_number_ratings=0, item_min_number_ratings=10)
            # #     URM_all = filter_urm(URM_all, user_min_number_ratings=2, item_min_number_ratings=0)
            # #
            # #     URM_train, URM_validation, URM_test, URM_negative = split_train_validation_test_negative_leave_one_out_user_wise(URM_all, negative_items_per_positive=999,
            # #                                                                                                                                          at_least_n_train_items_test=0, at_least_n_train_items_validation=0,
            # #                                                                                                                                          verbose=True)
            # #     URM_timestamp = sps.csc_matrix(([],([],[])), shape=URM_train.shape)
            #
            # else: # create from full dataset with leave out one time wise from ORIGINAL full dateset
            #     data_reader = GowallaReader_DataManager()
            #     loaded_dataset = data_reader.load_data()
            #
            #     URM_all = loaded_dataset.get_URM_all()
            #
            #     # use this function 2 time because the order could change slightly the number of final interactions
            #     # with this order we get the same number of interactions as in the paper
            #     URM_all = filter_urm(URM_all, user_min_number_ratings=0, item_min_number_ratings=10)
            #     URM_all = filter_urm(URM_all, user_min_number_ratings=2, item_min_number_ratings=0)
            #
            #     URM_timestamp = URM_all.copy()
            #     URM_all.data = np.ones_like(URM_all.data)
            #
            #     URM_train, URM_validation, URM_test, URM_negative = split_data_on_timestamp(URM_all, URM_timestamp, negative_items_per_positive=999)
            #     URM_train = URM_train + URM_validation
            #     URM_train, URM_validation = split_train_validation_leave_one_out_user_wise(URM_train, verbose=False)

            self.URM_DICT = {
                "URM_train": URM_train,
                "URM_test": URM_test,
                "URM_validation": URM_validation,
                "URM_test_negative": URM_test_negative,
            }

            save_data_dict_zip(self.URM_DICT, self.ICM_DICT, pre_splitted_path,
                               pre_splitted_filename)

        print("{}: Dataset loaded".format(self.DATASET_NAME))

        ut.print_stat_datareader(self)