def __init__(self, urm_train):
        super(HybridNorm3Recommender, self).__init__(urm_train)

        urm_train = check_matrix(urm_train.copy(), 'csr')
        self.num_users = urm_train.shape[0]

        # recommender_1 = HybridGenRecommender(urm_train)
        # recommender_1.fit()

        recommender_1 = RP3betaRecommender(urm_train)
        recommender_1.fit(topK=16,
                          alpha=0.03374950051351756,
                          beta=0.24087176329409027,
                          normalize_similarity=True)

        recommender_3 = UserKNNCFRecommender(urm_train)
        recommender_3.fit(shrink=2, topK=600, normalize=True)

        recommender_2 = ItemKNNCFRecommender(urm_train)
        recommender_2.fit(topK=5,
                          shrink=500,
                          feature_weighting='BM25',
                          similarity='tversky',
                          normalize=False,
                          tversky_alpha=0.0,
                          tversky_beta=1.0)

        self.recommender_1 = recommender_1
        self.recommender_2 = recommender_2
        self.recommender_3 = recommender_3
Exemplo n.º 2
0
class Hybrid002AlphaRecommender(BaseRecommender):
    """Hybrid000AlphaRecommender recommender"""

    RECOMMENDER_NAME = "Hybrid002AlphaRecommender"

    def __init__(self, URM_train, UCM, cold_users, warm_users):
        super(Hybrid002AlphaRecommender, self).__init__(URM_train)
        self.warm_recommender = UserKNNCFRecommender(URM_train)
        self.cold_recommender = UserKNNCBFRecommender(UCM, URM_train)
        self.cold_users = cold_users
        self.warm_users = warm_users

    def fit(self, random_seed=42, topK=14000, shrink=2):
        self.warm_recommender.fit(topK=topK,
                                  shrink=shrink,
                                  feature_weighting="BM25")
        self.cold_recommender.fit(topK=topK,
                                  shrink=shrink,
                                  feature_weighting="BM25")

    def recommend(self,
                  user_id_array,
                  cutoff=None,
                  remove_seen_flag=True,
                  items_to_compute=None,
                  remove_top_pop_flag=False,
                  remove_CustomItems_flag=False,
                  return_scores=False):
        if user_id_array in self.warm_users:
            return self.warm_recommender.recommend(user_id_array,
                                                   cutoff=cutoff)
        if user_id_array in self.cold_users:
            return self.cold_recommender.recommend(user_id_array,
                                                   cutoff=cutoff)
Exemplo n.º 3
0
    def fit(self,
            threshold_sim=0.5,
            threshold_count=5,
            topK=50,
            shrink=100,
            similarity='cosine',
            normalize=True,
            feature_weighting="none",
            **similarity_args):
        user_cf = UserKNNCFRecommender(self.URM_train)
        user_cf.fit(topK=topK,
                    shrink=shrink,
                    similarity=similarity,
                    normalize=normalize,
                    feature_weighting=feature_weighting,
                    **similarity_args)
        new_URM = VirtualNearestNeighbor.newAugmentUMR(
            URM_train=self.URM_train,
            W_sparse=user_cf.W_sparse,
            threshold_interactions=threshold_count,
            threshold_similarity=threshold_sim)

        new_item_cf = ItemKNNCFRecommender(new_URM)
        new_item_cf.fit(topK=topK,
                        shrink=shrink,
                        similarity=similarity,
                        normalize=normalize,
                        feature_weighting=feature_weighting,
                        **similarity_args)
        self.W_sparse = new_item_cf.W_sparse
Exemplo n.º 4
0
class Hybrid102AlphaRecommender(BaseRecommender):
    """Hybrid102AlphaRecommender recommender"""

    RECOMMENDER_NAME = "Hybrid102AlphaRecommender"

    def __init__(self, data: DataObject):
        super(Hybrid102AlphaRecommender, self).__init__(data.urm_train)
        self.data = data
        self.rec1 = UserKNNCFRecommender(data.urm_train)
        self.rec1.fit(topK=1000, shrink=4500, similarity="cosine", feature_weighting="TF-IDF")
        self.rec2 = ItemKNNCFRecommender(data.urm_train)
        self.rec2.fit(topK=2000, shrink=800, similarity="cosine", feature_weighting="TF-IDF")
        self.rec3 = SLIM_BPR_Cython(data.urm_train)
        self.rec3.fit(epochs=120, topK=800, lambda_i=0.1, lambda_j=0.1, learning_rate=0.0001)
        self.rec4 = RP3betaRecommender(data.urm_train)
        self.rec4.fit(topK=30, alpha=0.21, beta=0.25)
        target_users = data.urm_train_users_by_type[2][1]
        self.target_users = target_users
        self.hybrid_rec = Hybrid1CXAlphaRecommender(data, recommenders=[self.rec1, self.rec2, self.rec3],
                                                    recommended_users=target_users, max_cutoff=30)

    def fit(self):
        weights1 = np.array([[0.37625119, 0.43193487, 0.17444842, 0.16197883, 0.18204363,
                              0.17016599, 0.14983434, 0.11938279, 0.09980418, 0.1147748,
                              0.12762677, 0.08689066, 0.09533745, 0.10492991, 0.097475,
                              0.05278562, 0.05244627, 0.0602501, 0.06743845, 0.06145589,
                              0.07008017, 0.07410305, 0.07170746, 0.04231058, 0.04493697,
                              0.02516579, 0.0176046, 0.01360429, 0., 0.],
                             [0.55298149, 0.27456885, 0.2278579, 0.25095311, 0.24721051,
                              0.09937549, 0.09209609, 0.07158969, 0.07174988, 0.08251237,
                              0.09157335, 0.10530935, 0.1106961, 0.12150468, 0.12001527,
                              0.10052318, 0.09536568, 0.10770821, 0.08553278, 0.06198749,
                              0.05708056, 0.05176975, 0.05953521, 0.05567152, 0.06083775,
                              0.02776653, 0.02663699, 0.01181728, 0.01168978, 0.],
                             [0.25041731, 0.15536414, 0.16953122, 0.17164006, 0.11443169,
                              0.11873982, 0.07100542, 0.06452205, 0.06123626, 0.06430055,
                              0.06311274, 0.05618836, 0.05331187, 0.04611177, 0.04239514,
                              0.03824963, 0.04398116, 0.04738213, 0.04862799, 0.03962175,
                              0.04556502, 0.04738956, 0.054498, 0.0626727, 0.04973429,
                              0.03219802, 0.03227312, 0.0307041, 0.03396853, 0.],
                             [1., 0.5538608, 0.44692181, 0.20321725, 0.22012478,
                              0.1873366, 0.14329206, 0.09783222, 0.10765581, 0.10658318,
                              0.12257066, 0.13699397, 0.15743225, 0.12181424, 0.13897041,
                              0.08672218, 0.09188654, 0.05170634, 0.04459521, 0.04785834,
                              0.05248675, 0.06035977, 0.06733202, 0.06760871, 0.07775002,
                              0.0720465, 0.05977294, 0.04260028, 0.00546561, 0.0055422]])
        self.hybrid_rec.weights = weights1

    def recommend(self, user_id_array, cutoff=None, remove_seen_flag=True, items_to_compute=None,
                  remove_top_pop_flag=False, remove_CustomItems_flag=False, return_scores=False):
        if user_id_array in self.target_users:
            return self.hybrid_rec.recommend(user_id_array=user_id_array, cutoff=cutoff)
        else:
            return []
    def __init__(self, urm_train, eurm=False):
        super(HybridNorm2Recommender, self).__init__(urm_train)
        self.data_folder = Path(__file__).parent.parent.absolute()
        self.eurm = eurm
        self.num_users = urm_train.shape[0]

        urm_train = check_matrix(urm_train.copy(), 'csr')

        recommender_2 = ItemKNNCFRecommender(urm_train)
        recommender_2.fit(topK=5, shrink=500, feature_weighting='BM25', similarity='tversky', normalize=False, tversky_alpha=0.0, tversky_beta=1.0)

        recommender_3 = UserKNNCFRecommender(urm_train)
        recommender_3.fit(shrink=2, topK=600, normalize=True)
        # recommender_3 = UserKNNCFRecommender(urm_train)
        # recommender_3.fit(topK=697, shrink=1000, feature_weighting='TF-IDF', similarity='tversky', normalize=False,
        #                   tversky_alpha=1.0, tversky_beta=1.0)

        recommender_4 = RP3betaRecommender(urm_train)
        recommender_4.fit(topK=16, alpha=0.03374950051351756, beta=0.24087176329409027, normalize_similarity=True)

        recommender_5 = SLIM_BPR_Cython(urm_train)
        recommender_5.fit(lambda_i=0.0926694015, lambda_j=0.001697250, learning_rate=0.002391, epochs=65, topK=200)

        recommender_6 = ALSRecommender(urm_train)
        recommender_6.fit(alpha=5, iterations=40, reg=0.3)


        self.recommender_2 = recommender_2
        self.recommender_3 = recommender_3
        self.recommender_4 = recommender_4
        self.recommender_5 = recommender_5
        self.recommender_6 = recommender_6

        if self.eurm:

            self.score_matrix_1 = sps.load_npz(self.data_folder / 'Data/icm_sparse.npz')
            self.score_matrix_2 = self.recommender_2._compute_item_matrix_score(np.arange(self.num_users))
            self.score_matrix_3 = self.recommender_3._compute_item_matrix_score(np.arange(self.num_users))
            self.score_matrix_4 = self.recommender_4._compute_item_matrix_score(np.arange(self.num_users))
            self.score_matrix_5 = self.recommender_5._compute_item_matrix_score(np.arange(self.num_users))
            self.score_matrix_6 = self.recommender_6._compute_item_score(np.arange(self.num_users))
            self.score_matrix_7 = sps.load_npz(self.data_folder / 'Data/ucm_sparse.npz')

            self.score_matrix_1 = normalize(self.score_matrix_1, norm='l2', axis=1)
            self.score_matrix_2 = normalize(self.score_matrix_2, norm='l2', axis=1)
            self.score_matrix_3 = normalize(self.score_matrix_3, norm='l2', axis=1)
            self.score_matrix_4 = normalize(self.score_matrix_4, norm='l2', axis=1)
            self.score_matrix_5 = normalize(self.score_matrix_5, norm='l2', axis=1)
            self.score_matrix_6 = normalize(self.score_matrix_6, norm='l2', axis=1)
            self.score_matrix_7 = normalize(self.score_matrix_7, norm='l2', axis=1)
Exemplo n.º 6
0
 def __init__(self, data: DataObject):
     super(Hybrid102AlphaRecommender, self).__init__(data.urm_train)
     self.data = data
     self.rec1 = UserKNNCFRecommender(data.urm_train)
     self.rec1.fit(topK=1000, shrink=4500, similarity="cosine", feature_weighting="TF-IDF")
     self.rec2 = ItemKNNCFRecommender(data.urm_train)
     self.rec2.fit(topK=2000, shrink=800, similarity="cosine", feature_weighting="TF-IDF")
     self.rec3 = SLIM_BPR_Cython(data.urm_train)
     self.rec3.fit(epochs=120, topK=800, lambda_i=0.1, lambda_j=0.1, learning_rate=0.0001)
     self.rec4 = RP3betaRecommender(data.urm_train)
     self.rec4.fit(topK=30, alpha=0.21, beta=0.25)
     target_users = data.urm_train_users_by_type[2][1]
     self.target_users = target_users
     self.hybrid_rec = Hybrid1CXAlphaRecommender(data, recommenders=[self.rec1, self.rec2, self.rec3],
                                                 recommended_users=target_users, max_cutoff=30)
Exemplo n.º 7
0
    def __init__(self, urm_train):
        super(HybridNorm1Recommender, self).__init__(urm_train)
        self.num_users = urm_train.shape[0]
        urm_train = check_matrix(urm_train.copy(), 'csr')

        recommender_1 = HybridGen2Recommender(urm_train)
        recommender_1.fit()

        recommender_2 = ItemKNNCFRecommender(urm_train)
        recommender_2.fit(topK=5,
                          shrink=500,
                          feature_weighting='BM25',
                          similarity='tversky',
                          normalize=False,
                          tversky_alpha=0.0,
                          tversky_beta=1.0)

        recommender_3 = UserKNNCFRecommender(urm_train)
        recommender_3.fit(topK=697,
                          shrink=1000,
                          feature_weighting='TF-IDF',
                          similarity='tversky',
                          normalize=False,
                          tversky_alpha=1.0,
                          tversky_beta=1.0)

        recommender_4 = RP3betaRecommender(urm_train)
        recommender_4.fit(topK=16,
                          alpha=0.03374950051351756,
                          beta=0.24087176329409027,
                          normalize_similarity=True)

        recommmender_5 = SLIM_BPR_Cython(urm_train)
        recommmender_5.fit(lambda_i=0.0926694015,
                           lambda_j=0.001697250,
                           learning_rate=0.002391,
                           epochs=65,
                           topK=200)

        recommender_6 = ALSRecommender(urm_train)
        recommender_6.fit(alpha=5, iterations=40, reg=0.3)

        self.recommender_1 = recommender_1
        self.recommender_2 = recommender_2
        self.recommender_3 = recommender_3
        self.recommender_4 = recommender_4
        self.recommender_5 = recommmender_5
        self.recommender_6 = recommender_6
Exemplo n.º 8
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearHybrid002, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train

        # seed 1205: 'topK': 190, 'shrink': 0, 'similarity': 'cosine', 'normalize': True
        self.__rec1 = UserKNNCFRecommender(URM_train, verbose=False)
        self.__rec1_params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }

        # seed 1205: 'topK': 100, 'shrink': 1000, 'similarity': 'asymmetric', 'normalize': True, 'asymmetric_alpha': 0.0
        self.__rec2 = ItemKNNCFRecommender(URM_train, verbose=False)
        self.__rec2_params = {
            'topK': 100,
            'shrink': 1000,
            'similarity': 'asymmetric',
            'normalize': True,
            'asymmetric_alpha': 0.0
        }

        # seed 1205: 'topK': 205, 'shrink': 1000, 'similarity': 'cosine', 'normalize': True, 'feature_weighting': 'BM25'
        self.__rec3 = ItemKNNCBFRecommender(URM_train,
                                            ICM_train,
                                            verbose=False)
        self.__rec3_params = {
            'topK': 205,
            'shrink': 1000,
            'similarity': 'cosine',
            'normalize': True,
            'feature_weighting': 'BM25'
        }

        self.__a = self.__b = self.__c = None
        self.seed = seed
        self.__submission = submission
Exemplo n.º 9
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearHybrid008, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train

        self.__rec1 = SSLIMElasticNet(URM_train, ICM_train, verbose=False)
        self.__rec1_params = {
            'beta': 0.4849594591575789,
            'topK': 1000,
            'l1_ratio': 1e-05,
            'alpha': 0.001
        }

        self.__rec2 = ItemKNNCBFRecommender(URM_train,
                                            ICM_train,
                                            verbose=False)
        self.__rec2_params = {
            'topK': 65,
            'shrink': 0,
            'similarity': 'dice',
            'normalize': True
        }

        self.__rec3 = UserKNNCFRecommender(URM_train, verbose=False)
        self.__rec3_params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }

        self.__a = self.__b = self.__c = None
        self.seed = seed
        self.__submission = submission
def trial_boost_user(data):
    rec1 = UserKNNCFRecommender(data.urm_train)
    rec2 = NewUserKNNAgeRecommender(data)
    rec3 = UserKNNCFRecommender(data.urm_train)
    rec1.fit(topK=500, shrink=1000, similarity='cosine', feature_weighting='TF-IDF')
    rec2.set_similarity([1])
    rec2.fit(topK=0)

    for alpha in [0.999990, 0.999995, 0.999999]:
        print("alpha:" + str(alpha))
        rec3.W_sparse = alpha * rec1.W_sparse + (1 - alpha) * rec2.W_sparse
        total_evaluation(rec3)
Exemplo n.º 11
0
    def eval(self, *args):
        # Input parameters
        topK = args[0][0]
        shrink = args[0][1]
        similarity = args[0][2]
        normalize = args[0][3]
        feature_weighting = args[0][4]

        # Text used for csv file
        input_as_string = f"topK={topK} - shrink={shrink} - similarity={similarity} - normalize={normalize} - " \
                          f"feature_weighting={feature_weighting}"
        recommender_name = "Item CF"

        # Creating the recommenders (parallel fit and evaluation)
        recs = [
            UserKNNCFRecommender(data.urm_train) for data in self.dataset_list
        ]
        pairs = zip(recs, self.dataset_list)
        results = Parallel(n_jobs=parallelism)(
            delayed(parallel_fit_and_eval_job)
            (rec, data, topK, shrink, similarity, normalize, feature_weighting)
            for rec, data in pairs)

        # Computing the average MAP
        map_per_type = np.array(results).mean(axis=0)

        # Storing the information on file
        f = open(self.filename_csv, "a+")
        map_as_string = " ".join([str(x) + "," for x in map_per_type])
        f.write(f"{recommender_name}, {input_as_string}, {map_as_string}\n")
        f.flush()
        f.close()

        # The MAP value that should be optimized
        optimized_map = map_per_type[self.type_of_user]

        # Printing stuffs
        current_time = time.time()
        print(
            f"run : {self.counter} - computed in {current_time - self.timer} seconds"
        )
        print(f"\tparameters : {input_as_string}")
        print(f"\tmap : {optimized_map}\n")
        self.counter += 1
        self.timer = current_time

        return -optimized_map
Exemplo n.º 12
0
    def __init__(self, URM_train, ICM_all, verbose=True):
        super(UserWiseHybrid001, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_all = ICM_all

        # range and recommender definition ---> element structure: ( (start, end), recommender, fit_args* )
        self.__recommender_segmentation = [
            ((0, 1), TopPop(URM_train), {}),
            ((1, 25), P3alphaRecommender(URM_train),
             {'topK': 729, 'alpha': 0.4104229220476686, 'normalize_similarity': False}),
            ((25, 50), RP3betaRecommender(URM_train),
             {'topK': 939, 'alpha': 0.6073516078011799, 'beta': 0.002238854541773972, 'normalize_similarity': False}),
            ((50, 100), UserKNNCFRecommender(URM_train),
             {'topK': 90, 'shrink': 77, 'similarity': 'cosine', 'normalize': True}),
            ((100, 200), RP3betaRecommender(URM_train),
             {'topK': 1000, 'alpha': 0.32110178834628456, 'beta': 0.0, 'normalize_similarity': True}),
            ((200, -1), SLIM_BPR_Cython(URM_train),
             {'topK': 120, 'epochs': 20, 'symmetric': True, 'sgd_mode': 'adam', 'lambda_i': 0.01, 'lambda_j': 1e-05, 'learning_rate': 0.0001}),
        ]

        self.__loaded = {}
        for f_range, _, _ in self.__recommender_segmentation:
            self.__loaded[f_range] = False
Exemplo n.º 13
0
class LinearHybrid003(BaseItemSimilarityMatrixRecommender):
    RECOMMENDER_NAME = "LinearHybrid003"

    # set the seed equal to the one of the parameter search!!!!
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearHybrid003, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train

        # seed 1205: 'topK': 190, 'shrink': 0, 'similarity': 'cosine', 'normalize': True
        self.__rec1 = UserKNNCFRecommender(URM_train, verbose=False)
        self.__rec1_params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }

        self.__rec2 = P3alphaRecommender(URM_train, verbose=False)
        self.__rec2_params = {
            'topK': 131,
            'alpha': 0.33660811631883863,
            'normalize_similarity': False
        }

        # seed 1205: 'topK': 205, 'shrink': 1000, 'similarity': 'cosine', 'normalize': True, 'feature_weighting': 'BM25'
        self.__rec3 = ItemKNNCBFRecommender(URM_train,
                                            ICM_train,
                                            verbose=False)
        self.__rec3_params = {
            'topK': 205,
            'shrink': 1000,
            'similarity': 'cosine',
            'normalize': True,
            'feature_weighting': 'BM25'
        }

        self.__a = self.__b = self.__c = None
        self.seed = seed
        self.__submission = submission

    def fit(self, alpha=0.5, l1_ratio=0.5):
        self.__a = alpha * l1_ratio
        self.__b = alpha - self.__a
        self.__c = 1 - self.__a - self.__b
        if not self.__submission:
            try:
                self.__rec1.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec1.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec1.RECOMMENDER_NAME} ...")
                self.__rec1.fit(**self.__rec1_params)
                print(f"done.")
                self.__rec1.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')

            try:
                self.__rec2.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec2.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec2.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec2.RECOMMENDER_NAME} ...")
                self.__rec2.fit(**self.__rec2_params)
                print(f"done.")
                self.__rec2.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec2.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')

            try:
                self.__rec3.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec3.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec3.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec3.RECOMMENDER_NAME} ...")
                self.__rec3.fit(**self.__rec3_params)
                print(f"done.")
                self.__rec3.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec3.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
        else:
            self.__rec1.fit(**self.__rec1_params)
            self.__rec2.fit(**self.__rec2_params)
            self.__rec3.fit(**self.__rec3_params)

    def _compute_item_score(self, user_id_array, items_to_compute=None):

        item_weights_1 = self.__rec1._compute_item_score(user_id_array)
        item_weights_2 = self.__rec2._compute_item_score(user_id_array)
        item_weights_3 = self.__rec3._compute_item_score(user_id_array)

        item_weights = item_weights_1 * self.__a + item_weights_2 * self.__b + item_weights_3 * self.__c

        return item_weights

    def save_model(self, folder_path, file_name=None):
        if file_name is None:
            file_name = self.RECOMMENDER_NAME
        self._print("Saving model in file '{}'".format(folder_path +
                                                       file_name))
        dataIO = DataIO(folder_path=folder_path)
        dataIO.save_data(file_name=file_name, data_dict_to_save={})
        self._print("Saving complete")
Exemplo n.º 14
0
    def __init__(self, URM_train, ICM_train, submission=False, verbose=True, seed=1205):
        super(UserWiseHybrid008, self).__init__(URM_train, verbose=verbose)
        recommenders = {
            'rp3b': RP3betaRecommender(URM_train),
            'p3a': P3alphaRecommender(URM_train),
            'sen': SLIMElasticNetRecommender(URM_train),
            'sbpr': SLIM_BPR_Cython(URM_train),
            'icb': ItemKNNCBFRecommender(URM_train,ICM_train),
            'icf': ItemKNNCFRecommender(URM_train),
            'ucf': UserKNNCFRecommender(URM_train)
        }
        #print("Fitting rp3b...")
        #params = {'topK': 1000, 'alpha': 0.38192761611274967, 'beta': 0.0, 'normalize_similarity': False}
        #recommenders['rp3b'].fit(**params)
        #print("done.")
        print("Fitting p3a...")
        params = {'topK': 131, 'alpha': 0.33660811631883863, 'normalize_similarity': False}
        recommenders['p3a'].fit(**params)
        print("done.")
        print("Fitting sen...")
        params = {'topK': 992, 'l1_ratio': 0.004065081925341167, 'alpha': 0.003725005053334143}
        recommenders['sen'].fit(**params)
        print("done.")
        print("Fitting sbpr...")
        params = {'topK': 979, 'epochs': 130, 'symmetric': False, 'sgd_mode': 'adam', 'lambda_i': 0.004947329669424629,
                  'lambda_j': 1.1534760845071758e-05, 'learning_rate': 0.0001}
        recommenders['sbpr'].fit(**params)
        print("done.")
        print("Fitting icb...")
        params = {'topK': 65, 'shrink': 0, 'similarity': 'dice', 'normalize': True}
        recommenders['icb'].fit(**params)
        print("done.")
        print("Fitting icf...")
        params = {'topK': 55, 'shrink': 1000, 'similarity': 'asymmetric', 'normalize': True, 'asymmetric_alpha': 0.0}
        recommenders['icf'].fit(**params)
        print("done.")
        print("Fitting ucf...")
        params = {'topK': 190, 'shrink': 0, 'similarity': 'cosine', 'normalize': True}
        recommenders['ucf'].fit(**params)
        print("done.")

        self.__recommender_segmentation = [
            ((0,6), HiddenRecommender(URM_train, ICM_train, [
                recommenders['p3a'],
                recommenders['ucf'],
                recommenders['icb']
            ], submission=submission, verbose=verbose, seed=seed),
             {'alpha': 0.3987236515679141, 'l1_ratio': 0.15489605895390016}),

            ((6,16), HiddenRecommender(URM_train, ICM_train, [
                recommenders['ucf'],
                recommenders['icb'],
                recommenders['sen']
            ], submission=submission, verbose=verbose, seed=seed),
             {'alpha': 0.33535858857401674, 'l1_ratio': 0.4046400351885727}),

            ((16,-1), HiddenRecommender(URM_train, ICM_train, [
                recommenders['icb'],
                recommenders['sen'],
                recommenders['sbpr']
            ], submission=submission, verbose=verbose, seed=seed),
             {'alpha': 0.7321778261479165, 'l1_ratio': 0.15333729621089734}),
        ]
Exemplo n.º 15
0
from FeatureWeighting.User_CFW_D_Similarity_Linalg import User_CFW_D_Similarity_Linalg
from Hybrid.HybridNorm3Recommender import HybridNorm3Recommender
from MatrixFactorization.ALSRecommender import ALSRecommender
from MatrixFactorization.BPRRecommender import BPRRecommender
import similaripy as sim

data = DataManager()
urm_train = data.get_urm()

urm_train, urm_test = split_train_leave_k_out_user_wise(data.get_urm(),
                                                        temperature='normal')
urm_train, urm_valid = split_train_leave_k_out_user_wise(urm_train,
                                                         temperature='valid2')

urm_train_warm = data.create_test_warm_users(urm_train, threshold=10)
urm_test_warm = data.create_test_warm_users(urm_test, threshold=10)

evaluator_test_warm = EvaluatorHoldout(urm_test_warm, cutoff_list=[10])

recommender = UserKNNCFRecommender(urm_train)
recommender.fit(shrink=2, topK=600, normalize=True)

recommender_warm = UserKNNCFRecommender(urm_train_warm)
recommender_warm.fit(shrink=2, topK=500, normalize=True)

result, str_result = evaluator_test_warm.evaluateRecommender(recommender)
print('The Map of test of urm normal is : {}'.format(result[10]['MAP']))

result, str_result = evaluator_test_warm.evaluateRecommender(recommender_warm)
print('The Map of test of urm warm is : {}'.format(result[10]['MAP']))
Exemplo n.º 16
0
print('URM_train.shape:', URM_train.shape)
print('URM_test.shape:', URM_test.shape)
print('URM_validation.shape:', URM_validation.shape)

print('number_of_users:', number_of_users)
print('number_of_items:', number_of_items)

# test(URM_train, 267)

cutoff = 5

print_statistics(URM_train)

CMN_wrapper_train = CMN_RecommenderWrapper(URM_train)
user_KNNCF_Recommender = UserKNNCFRecommender(URM_train)
item_KNNCF_Recommender = ItemKNNCFRecommender(URM_train)
rp3_beta_Recommender = RP3betaRecommender(URM_train)

evaluator_negative_item_sample = EvaluatorNegativeItemSample(
    URM_test, URM_test_negative, cutoff_list=[5, 10])

CMN_wrapper_train.loadModel('result_experiments/SIGIR/CMN_pinterest/',
                            'CMN_RecommenderWrapper_best_model')
d, s = evaluator_negative_item_sample.evaluateRecommender(CMN_wrapper_train)
print('CMN_wrapper_train')
print(s)

user_KNNCF_Recommender.loadModel('result_experiments/SIGIR/CMN_pinterest/',
                                 'UserKNNCFRecommender_cosine_best_model')
d, s = evaluator_negative_item_sample.evaluateRecommender(
Exemplo n.º 17
0
    URM_train, URM_test = split_train_in_two_percentage_global_sample(URM_all, train_percentage=0.90, seed=seed)

    evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[10])

    sslim = SSLIMElasticNet(URM_train, ICM_all, verbose=False)
    sslim_params = {'beta': 0.4849594591575789, 'topK': 1000, 'l1_ratio': 1e-05, 'alpha': 0.001}
    try:
        sslim.load_model(f'stored_recommenders/seed_1205_S-SLIMElasticNet/', 'for_notebook_analysis')
        print(f"{sslim.RECOMMENDER_NAME} loaded.")
    except:
        print(f"Fitting {sslim.RECOMMENDER_NAME} ...")
        sslim.fit(**sslim_params)
        print(f"done.")
        sslim.save_model(f'stored_recommenders/seed_{str(seed)}_{sslim.RECOMMENDER_NAME}/', 'for_notebook_analysis')

    ucf = UserKNNCFRecommender(URM_train, verbose=False)
    ucf_params = {'topK': 190, 'shrink': 0, 'similarity': 'cosine', 'normalize': True}
    try:
        ucf.load_model(f'stored_recommenders/seed_{str(seed)}_{ucf.RECOMMENDER_NAME}/', 'for_notebook_analysis')
        print(f"{ucf.RECOMMENDER_NAME} loaded.")
    except:
        print(f"Fitting {ucf.RECOMMENDER_NAME} ...")
        ucf.fit(**ucf_params)
        print(f"done.")
        ucf.save_model(f'stored_recommenders/seed_{str(seed)}_{ucf.RECOMMENDER_NAME}/', 'for_notebook_analysis')

    icb = ItemKNNCBFRecommender(URM_train, ICM_all, verbose=False)

    icb_params = {'topK': 65, 'shrink': 0, 'similarity': 'dice', 'normalize': True}
    try:
        icb.load_model(f'stored_recommenders/seed_{str(seed)}_{icb.RECOMMENDER_NAME}/', 'for_notebook_analysis')
# recommenderELASTIC.save_model('model/', file_name='SLIM_ElasticNet_max')
recommenderELASTIC.load_model('model/',
                              file_name='SLIM_ElasticNet_l1ratio_0_5')

# recommenderAlphaGRAPH = P3alphaRecommender(URM_all)
# recommenderAlphaGRAPH.fit(topK=10, alpha=0.41, implicit=True, normalize_similarity=True)

recommenderBetaGRAPH = RP3betaRecommender(URM_all)
recommenderBetaGRAPH.fit(topK=54,
                         implicit=True,
                         normalize_similarity=True,
                         alpha=1e-6,
                         beta=0.2,
                         min_rating=0)

recommenderUserKNN = UserKNNCFRecommender(URM_all)
recommenderUserKNN.fit(topK=550,
                       shrink=0,
                       similarity='jaccard',
                       normalize=True)

recommenderIALS = IALSRecommender(URM_all)
# recommenderIALS.fit(epochs=200, alpha=1, reg=1e-4)
# recommenderIALS.save_model('model/', file_name='IALS_1_200_max')
recommenderIALS.load_model('model/', file_name='IALS_1_200_max')

recommenderUserCBF = UserKNNCBFRecommender(URM_all, UCM_all)
recommenderUserCBF.fit(topK=3000,
                       shrink=10,
                       similarity='cosine',
                       normalize=True)
Exemplo n.º 19
0
class LinearOverMerged001(BaseItemSimilarityMatrixRecommender):
    RECOMMENDER_NAME = "LinearOverMerged001"

    # set the seed equal to the one of the parameter search!!!!
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearOverMerged001, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train
        self.__submission = submission
        self.__rec1 = UserKNNCFRecommender(URM_train, verbose=False)
        self.__rec1_params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }
        self.seed = seed

        icb = ItemKNNCBFRecommender(URM_train, ICM_train, verbose=False)
        icb_params = {
            'topK': 65,
            'shrink': 0,
            'similarity': 'dice',
            'normalize': True
        }
        rp3b = RP3betaRecommender(URM_train, verbose=False)
        rp3b_params = {
            'topK': 1000,
            'alpha': 0.38192761611274967,
            'beta': 0.0,
            'normalize_similarity': False
        }
        sen = SLIMElasticNetRecommender(URM_train, verbose=False)
        sen_params = {
            'topK': 992,
            'l1_ratio': 0.004065081925341167,
            'alpha': 0.003725005053334143
        }

        if not self.__submission:
            try:
                icb.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{icb.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{icb.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {icb.RECOMMENDER_NAME} ...")
                icb.fit(**icb_params)
                print(f"done.")
                icb.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{icb.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
            try:
                rp3b.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{rp3b.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{rp3b.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {rp3b.RECOMMENDER_NAME} ...")
                rp3b.fit(**rp3b_params)
                print(f"done.")
                rp3b.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{rp3b.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
            try:
                sen.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{sen.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{sen.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {sen.RECOMMENDER_NAME} ...")
                sen.fit(**sen_params)
                print(f"done.")
                sen.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{sen.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
        else:
            icb.fit(**icb_params)
            rp3b.fit(**rp3b_params)
            sen.fit(**sen_params)

        self.__rec2 = HiddenMergedRecommender(URM_train,
                                              ICM_train, [icb, rp3b, sen],
                                              verbose=False)
        self.__rec2_params = {
            'alpha': 0.6355738550417837,
            'l1_ratio': 0.6617849709204384,
            'topK': 538
        }

        self.__a = self.__b = None

    def fit(self, alpha=0.5):
        self.__a = alpha
        self.__b = 1 - alpha
        if not self.__submission:
            try:
                self.__rec1.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec1.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec1.RECOMMENDER_NAME} ...")
                self.__rec1.fit(**self.__rec1_params)
                print(f"done.")
                self.__rec1.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')

        else:
            self.__rec1.fit(**self.__rec1_params)

        self.__rec2.fit(**self.__rec2_params)

    def _compute_item_score(self, user_id_array, items_to_compute=None):

        item_weights_1 = self.__rec1._compute_item_score(user_id_array)
        item_weights_2 = self.__rec2._compute_item_score(user_id_array)

        item_weights = item_weights_1 * self.__a + item_weights_2 * self.__b

        return item_weights

    def save_model(self, folder_path, file_name=None):
        if file_name is None:
            file_name = self.RECOMMENDER_NAME
        self._print("Saving model in file '{}'".format(folder_path +
                                                       file_name))
        dataIO = DataIO(folder_path=folder_path)
        dataIO.save_data(file_name=file_name, data_dict_to_save={})
        self._print("Saving complete")
Exemplo n.º 20
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearOverMerged001, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train
        self.__submission = submission
        self.__rec1 = UserKNNCFRecommender(URM_train, verbose=False)
        self.__rec1_params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }
        self.seed = seed

        icb = ItemKNNCBFRecommender(URM_train, ICM_train, verbose=False)
        icb_params = {
            'topK': 65,
            'shrink': 0,
            'similarity': 'dice',
            'normalize': True
        }
        rp3b = RP3betaRecommender(URM_train, verbose=False)
        rp3b_params = {
            'topK': 1000,
            'alpha': 0.38192761611274967,
            'beta': 0.0,
            'normalize_similarity': False
        }
        sen = SLIMElasticNetRecommender(URM_train, verbose=False)
        sen_params = {
            'topK': 992,
            'l1_ratio': 0.004065081925341167,
            'alpha': 0.003725005053334143
        }

        if not self.__submission:
            try:
                icb.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{icb.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{icb.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {icb.RECOMMENDER_NAME} ...")
                icb.fit(**icb_params)
                print(f"done.")
                icb.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{icb.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
            try:
                rp3b.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{rp3b.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{rp3b.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {rp3b.RECOMMENDER_NAME} ...")
                rp3b.fit(**rp3b_params)
                print(f"done.")
                rp3b.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{rp3b.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
            try:
                sen.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{sen.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{sen.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {sen.RECOMMENDER_NAME} ...")
                sen.fit(**sen_params)
                print(f"done.")
                sen.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{sen.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
        else:
            icb.fit(**icb_params)
            rp3b.fit(**rp3b_params)
            sen.fit(**sen_params)

        self.__rec2 = HiddenMergedRecommender(URM_train,
                                              ICM_train, [icb, rp3b, sen],
                                              verbose=False)
        self.__rec2_params = {
            'alpha': 0.6355738550417837,
            'l1_ratio': 0.6617849709204384,
            'topK': 538
        }

        self.__a = self.__b = None
Exemplo n.º 21
0
class LinearHybrid008(BaseItemSimilarityMatrixRecommender):
    RECOMMENDER_NAME = "LinearHybrid008"

    # set the seed equal to the one of the parameter search!!!!
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearHybrid008, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train

        self.__rec1 = SSLIMElasticNet(URM_train, ICM_train, verbose=False)
        self.__rec1_params = {
            'beta': 0.4849594591575789,
            'topK': 1000,
            'l1_ratio': 1e-05,
            'alpha': 0.001
        }

        self.__rec2 = ItemKNNCBFRecommender(URM_train,
                                            ICM_train,
                                            verbose=False)
        self.__rec2_params = {
            'topK': 65,
            'shrink': 0,
            'similarity': 'dice',
            'normalize': True
        }

        self.__rec3 = UserKNNCFRecommender(URM_train, verbose=False)
        self.__rec3_params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }

        self.__a = self.__b = self.__c = None
        self.seed = seed
        self.__submission = submission

    def fit(self, alpha=0.5, l1_ratio=0.5):
        self.__a = alpha * l1_ratio
        self.__b = alpha - self.__a
        self.__c = 1 - self.__a - self.__b
        if not self.__submission:
            try:
                self.__rec1.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec1.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec1.RECOMMENDER_NAME} ...")
                self.__rec1.fit(**self.__rec1_params)
                print(f"done.")
                self.__rec1.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')

            try:
                self.__rec2.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec2.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec2.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec2.RECOMMENDER_NAME} ...")
                self.__rec2.fit(**self.__rec2_params)
                print(f"done.")
                self.__rec2.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec2.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')

            try:
                self.__rec3.load_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec3.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
                print(f"{self.__rec3.RECOMMENDER_NAME} loaded.")
            except:
                print(f"Fitting {self.__rec3.RECOMMENDER_NAME} ...")
                self.__rec3.fit(**self.__rec3_params)
                print(f"done.")
                self.__rec3.save_model(
                    f'stored_recommenders/seed_{str(self.seed)}_{self.__rec3.RECOMMENDER_NAME}/',
                    f'best_for_{self.RECOMMENDER_NAME}')
        else:
            self.__rec1.fit(**self.__rec1_params)
            self.__rec2.fit(**self.__rec2_params)
            self.__rec3.fit(**self.__rec3_params)

    def _compute_item_score(self, user_id_array, items_to_compute=None):

        item_weights_1 = self.__rec1._compute_item_score(user_id_array)
        item_weights_2 = self.__rec2._compute_item_score(user_id_array)
        item_weights_3 = self.__rec3._compute_item_score(user_id_array)

        # normalization
        item_weights_1_max = item_weights_1.max()
        item_weights_2_max = item_weights_2.max()
        item_weights_3_max = item_weights_3.max()

        if not item_weights_1_max == 0:
            item_weights_1 /= item_weights_1_max
        if not item_weights_2_max == 0:
            item_weights_2 /= item_weights_2_max
        if not item_weights_3_max == 0:
            item_weights_3 /= item_weights_3_max

        item_weights = item_weights_1 * self.__a + item_weights_2 * self.__b + item_weights_3 * self.__c

        return item_weights

    def save_model(self, folder_path, file_name=None):
        if file_name is None:
            file_name = self.RECOMMENDER_NAME
        self._print("Saving model in file '{}'".format(folder_path +
                                                       file_name))
        dataIO = DataIO(folder_path=folder_path)
        dataIO.save_data(file_name=file_name, data_dict_to_save={})
        self._print("Saving complete")
Exemplo n.º 22
0
    def fit(self,
            topK=None,
            shrink=None,
            weights=None,
            pop=None,
            weights1=None,
            weights2=None,
            weights3=None,
            weights4=None,
            weights5=None,
            weights6=None,
            weights7=None,
            weights8=None,
            pop1=None,
            pop2=None,
            similarity='cosine',
            normalize=True,
            final_weights=None,
            final_weights1=None,
            final_weights2=None,
            old_similarity_matrix=None,
            epochs=1,
            top1=None,
            shrink1=None,
            force_compute_sim=False,
            weights_to_dweights=-1,
            **similarity_args):

        if topK is None:  # IT MEANS THAT I'M TESTING ONE RECOMMENDER ON A SPECIIFC INTERVAL
            topK = [top1]
            shrink = [shrink1]

        if self.weights is None:
            if weights1 is not None:
                weights = [
                    weights1, weights2, weights3, weights4, weights5, weights6,
                    weights7, weights8
                ]
                weights = [x for x in weights if x is not None]
            self.weights = weights

        if self.pop is None:
            if pop is None:
                pop = [pop1, pop2]
                pop = [x for x in pop if x is not None]
            self.pop = pop

        if weights_to_dweights != -1:
            self.d_weights[weights_to_dweights] = self.weights

        assert self.weights is not None, "Weights Are None!"

        assert len(self.recommender_list) == len(
            self.weights
        ), "Weights: {} and recommender list: {} have different lenghts".format(
            len(self.weights), len(self.recommender_list))

        assert len(topK) == len(shrink) == len(self.recommender_list), "Knns, Shrinks and recommender list have " \
                                                                       "different lenghts "

        self.normalize = normalize
        self.topK = topK
        self.shrink = shrink

        self.gradients = [0] * self.recommender_number
        self.MAE = 0
        p3counter = 0
        rp3bcounter = 0
        slim_counter = 0
        factorCounter = 0

        for knn, shrink, recommender in zip(topK, shrink,
                                            self.recommender_list):
            if recommender.__class__ is SLIM_BPR_Cython:
                if "lambda_i" in list(similarity_args.keys()
                                      ):  # lambda i and j provided in args
                    if type(similarity_args["lambda_i"]) is not list:
                        similarity_args["lambda_i"] = [
                            similarity_args["lambda_i"]
                        ]
                        similarity_args["lambda_j"] = [
                            similarity_args["lambda_j"]
                        ]
                    recommender.fit(
                        old_similarity_matrix=old_similarity_matrix,
                        epochs=epochs,
                        force_compute_sim=force_compute_sim,
                        topK=knn,
                        lambda_i=similarity_args["lambda_i"][slim_counter],
                        lambda_j=similarity_args["lambda_j"][slim_counter])
                else:
                    recommender.fit(
                        old_similarity_matrix=old_similarity_matrix,
                        epochs=epochs,
                        force_compute_sim=force_compute_sim,
                        topK=knn)
                slim_counter += 1

            elif recommender.__class__ in [
                    MatrixFactorization_BPR_Cython,
                    MatrixFactorization_FunkSVD_Cython,
                    MatrixFactorization_AsySVD_Cython
            ]:
                recommender.fit(epochs=epochs,
                                force_compute_sim=force_compute_sim)

            elif recommender.__class__ in [SLIMElasticNetRecommender]:
                recommender.fit(topK=knn,
                                l1_ratio=similarity_args["l1_ratio"],
                                force_compute_sim=force_compute_sim)

            elif recommender.__class__ in [PureSVDRecommender]:
                recommender.fit(
                    num_factors=similarity_args["num_factors"][factorCounter],
                    force_compute_sim=force_compute_sim)
                factorCounter += 1

            elif recommender.__class__ in [P3alphaRecommender]:
                if type(similarity_args["alphaP3"]) is not list:
                    similarity_args["alphaP3"] = [similarity_args["alphaP3"]]
                recommender.fit(topK=knn,
                                alpha=similarity_args["alphaP3"][p3counter],
                                min_rating=0,
                                implicit=True,
                                normalize_similarity=True,
                                force_compute_sim=force_compute_sim)
                p3counter += 1

            elif recommender.__class__ in [RP3betaRecommender]:
                recommender.fit(alpha=similarity_args["alphaRP3"][rp3bcounter],
                                beta=similarity_args["betaRP"][rp3bcounter],
                                min_rating=0,
                                topK=knn,
                                implicit=True,
                                normalize_similarity=True,
                                force_compute_sim=force_compute_sim)
                rp3bcounter += 1

            elif recommender.__class__ in [ItemKNNCBFRecommender]:
                recommender.fit(knn,
                                shrink,
                                force_compute_sim=force_compute_sim,
                                feature_weighting_index=similarity_args[
                                    "feature_weighting_index"])

            else:  # ItemCF, UserCF, ItemCBF, UserCBF
                recommender.fit(knn,
                                shrink,
                                force_compute_sim=force_compute_sim)

        print("Recommender list before: {}".format(self.recommender_list))
        self.W_sparse20 = csr_matrix(([], ([], [])), shape=(20635, 20635))
        self.W_sparse50 = csr_matrix(([], ([], [])), shape=(50446, 50446))
        to_delete = []
        for index, recommender in enumerate(self.recommender_list):
            try:
                self.W_sparse20 += self.weights[index] * recommender.W_sparse
                to_delete.append(recommender)
                print("Recommender {} is summed in W_sparse20".format(
                    recommender))
                continue
            except:
                # the recommender doesn't have a W sparse matrix of that shape
                a = 1

            try:
                self.W_sparse50 += self.weights[index] * recommender.W_sparse
                to_delete.append(recommender)
                print("Recommender {} is summed in W_sparse50".format(
                    recommender))
                continue
            except:
                # the recommender doesn't have a W sparse matrix of that shape
                a = 1

        # remove recommenders that already has the similarity merged
        self.recommender_list = [
            x for x in self.recommender_list if x not in to_delete
        ]

        print("Recommender list after: {}".format(self.recommender_list))
        new_item_recommender = ItemKNNCFRecommender(self.URM_train)
        new_item_recommender.W_sparse = self.W_sparse20
        self.recommender_list.append(new_item_recommender)

        new_user_recommender = UserKNNCFRecommender(self.URM_train)
        new_user_recommender.W_sparse = self.W_sparse50
        self.recommender_list.append(new_user_recommender)
        print("Recommender list final: {}".format(self.recommender_list))

        if final_weights is None:
            self.final_weights = [final_weights1, final_weights2]
        else:
            self.final_weights = final_weights

        assert len(final_weights) == len(
            self.recommender_list
        ), "Lunghezza di final weights e dei reccomender rimasti è diversa. Sono rimasti {} recommender con i final weight di lunghezza {}".format(
            len(self.recommender_list), len(final_weights))
Exemplo n.º 23
0
 def __init__(self, URM_train, UCM, cold_users, warm_users):
     super(Hybrid002AlphaRecommender, self).__init__(URM_train)
     self.warm_recommender = UserKNNCFRecommender(URM_train)
     self.cold_recommender = UserKNNCBFRecommender(UCM, URM_train)
     self.cold_users = cold_users
     self.warm_users = warm_users
Exemplo n.º 24
0
     (trackCol, artistIdCol)))  # sparse matrix ROW: track_id COLUMN: artist_id
matTrack_Artist = matTrack_Artist.tocsr()

# matTrack_Dur = sps.coo_matrix(((np.ones(numTrack, dtype=int)), (trackCol, durSecCol)))       #sparse matrix ROW: track_id COLUMN: duration_sec
# matTrack_Dur = matTrack_Dur.tocsr()

# NUOVA AGGIUNTA

reader = dataReader()
URM_train = reader.mat_complete
ICM_Art = matTrack_Artist
ICM_Alb = matTrack_Album

recommender = HybridRecommender(URM_train)
item = ItemKNNCFRecommender(URM_train)
user = UserKNNCFRecommender(URM_train)
SLIM = MultiThreadSLIM_ElasticNet(URM_train=URM_train)
item.fit(topK=800, shrink=10, similarity='cosine', normalize=True)
user.fit(topK=70, shrink=22, similarity='cosine', normalize=True)
SLIM.fit(l1_penalty=1e-05,
         l2_penalty=0,
         positive_only=True,
         topK=150,
         alpha=0.00415637376180466)
recommender.fit(ICM_Art,
                ICM_Alb,
                item=item,
                user=user,
                SLIM=SLIM,
                w_itemcf=1.1,
                w_usercf=0.6,
Exemplo n.º 25
0
    recommenderELASTIC = SLIMElasticNetRecommender(URM_train)
    # recommenderELASTIC.fit(topK=10)
    # recommenderELASTIC.save_model('model/', file_name='SLIM_ElasticNet')
    recommenderELASTIC.load_model('model/', file_name='SLIM_ElasticNet_train')

    # recommenderAlphaGRAPH = P3alphaRecommender(URM_train)
    # recommenderAlphaGRAPH.fit(topK=10, alpha=0.22, implicit=True, normalize_similarity=True)

    recommenderBetaGRAPH = RP3betaRecommender(URM_train)
    recommenderBetaGRAPH.fit(topK=10,
                             implicit=True,
                             normalize_similarity=True,
                             alpha=0.41,
                             beta=0.049)

    recommederUserKNN = UserKNNCFRecommender(URM_train)
    recommederUserKNN.fit(topK=10, shrink=15, similarity='jaccard')

    # Create BayesianSearch object
    recommender_class = ItemKNNScoresHybridRecommender

    parameterSearch = SearchBayesianSkopt(
        recommender_class,
        evaluator_validation=evaluator_validation,
        evaluator_test=evaluator_test)

    # weight_list = [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.8, 0.9]
    weight_list = [
        0.1, 0.3, 0.5, 0.7, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 7, 8, 9,
        10
    ]
Exemplo n.º 26
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(UserWiseHybrid005, self).__init__(URM_train, verbose=verbose)
        recommenders = {
            'rp3b': RP3betaRecommender(URM_train),
            'p3a': P3alphaRecommender(URM_train),
            'sen': SLIMElasticNetRecommender(URM_train),
            'sbpr': SLIM_BPR_Cython(URM_train),
            'icb': ItemKNNCBFRecommender(URM_train, ICM_train),
            'icf': ItemKNNCFRecommender(URM_train),
            'ucf': UserKNNCFRecommender(URM_train)
        }
        print("Fitting rp3b...")
        params = {
            'topK': 1000,
            'alpha': 0.38192761611274967,
            'beta': 0.0,
            'normalize_similarity': False
        }
        recommenders['rp3b'].fit(**params)
        print("done.")
        print("Fitting p3a...")
        params = {
            'topK': 131,
            'alpha': 0.33660811631883863,
            'normalize_similarity': False
        }
        recommenders['p3a'].fit(**params)
        print("done.")
        print("Fitting sen...")
        params = {
            'topK': 992,
            'l1_ratio': 0.004065081925341167,
            'alpha': 0.003725005053334143
        }
        recommenders['sen'].fit(**params)
        print("done.")
        #print("Fitting sbpr...")
        #params = {'topK': 979, 'epochs': 130, 'symmetric': False, 'sgd_mode': 'adam', 'lambda_i': 0.004947329669424629,
        #          'lambda_j': 1.1534760845071758e-05, 'learning_rate': 0.0001}
        #recommenders['sbpr'].fit(**params)
        print("done.")
        print("Fitting icb...")
        params = {
            'topK': 65,
            'shrink': 0,
            'similarity': 'dice',
            'normalize': True
        }
        recommenders['icb'].fit(**params)
        print("done.")
        print("Fitting icf...")
        params = {
            'topK': 55,
            'shrink': 1000,
            'similarity': 'asymmetric',
            'normalize': True,
            'asymmetric_alpha': 0.0
        }
        recommenders['icf'].fit(**params)
        print("done.")
        print("Fitting ucf...")
        params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }
        recommenders['ucf'].fit(**params)
        print("done.")

        self.__recommender_segmentation = [
            ((0, 3),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['rp3b'], recommenders['icf'],
                                   recommenders['icb']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.4577946628581237,
                                   'l1_ratio': 0.7434539743766688
                               }),
            ((3, 5),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['p3a'], recommenders['ucf'],
                                   recommenders['icb']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.3987236515679141,
                                   'l1_ratio': 0.15489605895390016
                               }),
            ((5, 10),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['rp3b'], recommenders['icb'],
                                   recommenders['sen']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 1.0,
                                   'l1_ratio': 0.3951763029766836
                               }),
            ((10, 17),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['p3a'], recommenders['icb'],
                                   recommenders['sen']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.9999772418587548,
                                   'l1_ratio': 0.28511052552468436
                               }),
            ((17, 30),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['icf'], recommenders['icb'],
                                   recommenders['sen']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.21686976560272436,
                                   'l1_ratio': 0.4598014054291886
                               }),
            ((30, 100),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['ucf'], recommenders['icb'],
                                   recommenders['sen']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.33535858857401674,
                                   'l1_ratio': 0.4046400351885727
                               }),
            ((100, 200),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['icf'], recommenders['icb'],
                                   recommenders['sen']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.21686976560272436,
                                   'l1_ratio': 0.4598014054291886
                               }),
            ((200, -1),
             HiddenRecommender(URM_train,
                               ICM_train, [
                                   recommenders['p3a'], recommenders['icb'],
                                   recommenders['sen']
                               ],
                               submission=submission,
                               verbose=verbose,
                               seed=seed), {
                                   'alpha': 0.9999772418587548,
                                   'l1_ratio': 0.28511052552468436
                               }),
        ]
Exemplo n.º 27
0
filename = P3alphaRecommender.RECOMMENDER_NAME \
           + ItemKNNCBFRecommender.RECOMMENDER_NAME \
           + ItemKNNCFRecommender.RECOMMENDER_NAME \
           + "hybrid_opt"

output_root_path += filename
output_file = open(output_root_path, "a")

P3alpha = P3alphaRecommender(URM_train)
P3alpha.fit(topK=100, alpha=0.7905462550621185, implicit=True, normalize_similarity=True)
# print("-------------------")
# print("--P3alpha fitted---")
# print("-------------------")

UserBased = UserKNNCFRecommender(URM_train)
UserBased.fit(topK=300, shrink=200)

ContentBased = ItemKNNCBFRecommender(ICM, URM_train)
ContentBased.fit(topK=50, shrink=100)
# print("-------------------")
# print("--KNNCBF fitted---")
# print("-------------------")
ItemKNNCF = ItemKNNCFRecommender(URM_train)
ItemKNNCF.fit(topK=300, shrink=100)
# print("-------------------")
# print("---KNNCF fitted----")
# print("-------------------")
PureSVD = PureSVDRecommender(URM_train)
PureSVD.fit(num_factors=240)
# print("-------------------")
Exemplo n.º 28
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(UserWiseHybrid009, self).__init__(URM_train, verbose=verbose)
        recommenders = {
            'rp3b': RP3betaRecommender(URM_train),
            'p3a': P3alphaRecommender(URM_train),
            'sen': SLIMElasticNetRecommender(URM_train),
            'sbpr': SLIM_BPR_Cython(URM_train),
            'icb': ItemKNNCBFRecommender(URM_train, ICM_train),
            'icf': ItemKNNCFRecommender(URM_train),
            'ucf': UserKNNCFRecommender(URM_train),
            'sslim': SSLIMElasticNet(URM_train, ICM_train)
        }

        params = {
            'topK': 1000,
            'alpha': 0.38192761611274967,
            'beta': 0.0,
            'normalize_similarity': False
        }
        try:
            recommenders['rp3b'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["rp3b"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['rp3b'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['rp3b'].RECOMMENDER_NAME} ...")
            recommenders['rp3b'].fit(**params)
            recommenders['rp3b'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["rp3b"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'topK': 131,
            'alpha': 0.33660811631883863,
            'normalize_similarity': False
        }
        try:
            recommenders['p3a'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["p3a"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['p3a'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['p3a'].RECOMMENDER_NAME} ...")
            recommenders['p3a'].fit(**params)
            recommenders['p3a'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["p3a"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'topK': 992,
            'l1_ratio': 0.004065081925341167,
            'alpha': 0.003725005053334143
        }
        try:
            recommenders['sen'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["sen"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['sen'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['sen'].RECOMMENDER_NAME} ...")
            recommenders['sen'].fit(**params)
            recommenders['sen'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["sen"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'topK': 979,
            'epochs': 130,
            'symmetric': False,
            'sgd_mode': 'adam',
            'lambda_i': 0.004947329669424629,
            'lambda_j': 1.1534760845071758e-05,
            'learning_rate': 0.0001
        }
        try:
            recommenders['sbpr'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["sbpr"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['sbpr'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['sbpr'].RECOMMENDER_NAME} ...")
            recommenders['sbpr'].fit(**params)
            recommenders['sbpr'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["sbpr"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'topK': 65,
            'shrink': 0,
            'similarity': 'dice',
            'normalize': True
        }
        try:
            recommenders['icb'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["icb"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['icb'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['icb'].RECOMMENDER_NAME} ...")
            recommenders['icb'].fit(**params)
            recommenders['icb'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["icb"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'topK': 55,
            'shrink': 1000,
            'similarity': 'asymmetric',
            'normalize': True,
            'asymmetric_alpha': 0.0
        }
        try:
            recommenders['icf'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["icf"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['icf'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['icf'].RECOMMENDER_NAME} ...")
            recommenders['icf'].fit(**params)
            recommenders['icf'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["icf"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'topK': 190,
            'shrink': 0,
            'similarity': 'cosine',
            'normalize': True
        }
        try:
            recommenders['ucf'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["ucf"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['ucf'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['ucf'].RECOMMENDER_NAME} ...")
            recommenders['ucf'].fit(**params)
            recommenders['ucf'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["ucf"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        params = {
            'beta': 0.4849594591575789,
            'topK': 1000,
            'l1_ratio': 1e-05,
            'alpha': 0.001
        }
        try:
            recommenders['sslim'].load_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["sslim"].RECOMMENDER_NAME}_for_sub')
            print(f"{recommenders['sslim'].RECOMMENDER_NAME} loaded.")
        except:
            print(f"Fitting {recommenders['sslim'].RECOMMENDER_NAME} ...")
            recommenders['sslim'].fit(**params)
            recommenders['sslim'].save_model(
                f'stored_recommenders/seed_{str(seed)}_hybrid_sub/',
                f'{recommenders["sslim"].RECOMMENDER_NAME}_for_sub')
            print(f"done.")

        self.__recommender_segmentation = [
            ((0, 3),
             HiddenMergedRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['rp3b'], recommenders['icb'],
                     recommenders['icf']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.7276553525851246,
                     'l1_ratio': 0.6891035546237165,
                     'topK': 1000
                 }),
            ((3, 5),
             HiddenLinearRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['sslim'], recommenders['p3a'],
                     recommenders['icb']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.9847198829156348,
                     'l1_ratio': 0.9996962519963414
                 }),
            ((5, 10),
             HiddenLinearRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['icb'], recommenders['rp3b'],
                     recommenders['sen']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.9949623682515907,
                     'l1_ratio': 0.007879399002699851
                 }),
            ((10, 17),
             HiddenLinearRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['sslim'], recommenders['icb'],
                     recommenders['ucf']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.6461624491197696,
                     'l1_ratio': 0.7617220099582368
                 }),
            ((17, 30),
             HiddenLinearRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['sslim'], recommenders['p3a'],
                     recommenders['icb']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.8416340030829476,
                     'l1_ratio': 0.6651408407090509
                 }),
            ((30, 100),
             HiddenLinearRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['sslim'], recommenders['icb'],
                     recommenders['icf']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.996772013761913,
                     'l1_ratio': 0.7831508517025596
                 }),
            ((100, 200),
             HiddenLinearRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['sslim'], recommenders['rp3b'],
                     recommenders['icb']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.8416340030829476,
                     'l1_ratio': 0.6651408407090509
                 }),
            ((200, -1),
             HiddenMergedRecommender(
                 URM_train,
                 ICM_train, [
                     recommenders['sslim'], recommenders['p3a'],
                     recommenders['icb']
                 ],
                 submission=submission,
                 verbose=verbose,
                 seed=seed), {
                     'alpha': 0.859343616443417,
                     'l1_ratio': 0.8995038091652459,
                     'topK': 900
                 }),
        ]
Exemplo n.º 29
0
    def __init__(self, urm_train, eurm=False):
        super(HybridNormOrigRecommender, self).__init__(urm_train)
        self.data_folder = Path(__file__).parent.parent.absolute()
        self.eurm = eurm
        self.num_users = urm_train.shape[0]

        data = DataManager()

        urm_train = check_matrix(urm_train.copy(), 'csr')
        icm_price, icm_asset, icm_sub, icm_all = data.get_icm()
        ucm_age, ucm_region, ucm_all = data.get_ucm()

        recommender_1 = ItemKNNCBFRecommender(urm_train, icm_all)
        recommender_1.fit(shrink=40, topK=20, feature_weighting='BM25')

        recommender_7 = UserKNNCBFRecommender(urm_train, ucm_all)
        recommender_7.fit(shrink=1777,
                          topK=1998,
                          similarity='tversky',
                          feature_weighting='BM25',
                          tversky_alpha=0.1604953616,
                          tversky_beta=0.9862348646)

        # recommender_1 = HybridGenRecommender(urm_train, eurm=self.eurm)
        # recommender_1.fit()

        # recommender_2 = ItemKNNCFRecommender(urm_train)
        # recommender_2.fit(shrink=30, topK=20)

        recommender_2 = ItemKNNCFRecommender(urm_train)
        recommender_2.fit(topK=5,
                          shrink=500,
                          feature_weighting='BM25',
                          similarity='tversky',
                          normalize=False,
                          tversky_alpha=0.0,
                          tversky_beta=1.0)

        recommender_3 = UserKNNCFRecommender(urm_train)
        recommender_3.fit(shrink=2, topK=600, normalize=True)
        # recommender_3 = UserKNNCFRecommender(urm_train)
        # recommender_3.fit(topK=697, shrink=1000, feature_weighting='TF-IDF', similarity='tversky', normalize=False,
        #                   tversky_alpha=1.0, tversky_beta=1.0)

        recommender_4 = RP3betaRecommender(urm_train)
        recommender_4.fit(topK=16,
                          alpha=0.03374950051351756,
                          beta=0.24087176329409027,
                          normalize_similarity=False)

        recommender_5 = SLIM_BPR_Cython(urm_train)
        recommender_5.fit(lambda_i=0.0926694015,
                          lambda_j=0.001697250,
                          learning_rate=0.002391,
                          epochs=65,
                          topK=200)

        recommender_6 = ALSRecommender(urm_train)
        recommender_6.fit(alpha=5, iterations=40, reg=0.3)

        self.recommender_1 = recommender_1
        self.recommender_2 = recommender_2
        self.recommender_3 = recommender_3
        self.recommender_4 = recommender_4
        self.recommender_5 = recommender_5
        self.recommender_6 = recommender_6
        self.recommender_7 = recommender_7

        if self.eurm:

            if Path(self.data_folder / 'Data/uicm_orig_sparse.npz').is_file():
                print("Previous uicm_sparse found")
                self.score_matrix_1 = sps.load_npz(self.data_folder /
                                                   'Data/uicm_sparse.npz')
            else:
                print("uicm_sparse not found, create new one...")
                self.score_matrix_1 = self.recommender_1._compute_item_matrix_score(
                    np.arange(self.num_users))
                sps.save_npz(self.data_folder / 'Data/uicm_orig_sparse.npz',
                             self.score_matrix_1)

            self.score_matrix_2 = self.recommender_2._compute_item_matrix_score(
                np.arange(self.num_users))
            self.score_matrix_3 = self.recommender_3._compute_item_matrix_score(
                np.arange(self.num_users))
            self.score_matrix_4 = self.recommender_4._compute_item_matrix_score(
                np.arange(self.num_users))
            self.score_matrix_5 = self.recommender_5._compute_item_matrix_score(
                np.arange(self.num_users))
            self.score_matrix_6 = self.recommender_6._compute_item_score(
                np.arange(self.num_users))

            self.score_matrix_1 = normalize(self.score_matrix_2,
                                            norm='max',
                                            axis=1)
            self.score_matrix_2 = normalize(self.score_matrix_2,
                                            norm='max',
                                            axis=1)
            self.score_matrix_3 = normalize(self.score_matrix_3,
                                            norm='max',
                                            axis=1)
            self.score_matrix_4 = normalize(self.score_matrix_4,
                                            norm='max',
                                            axis=1)
            self.score_matrix_5 = normalize(self.score_matrix_5,
                                            norm='max',
                                            axis=1)
            self.score_matrix_6 = normalize(self.score_matrix_6,
                                            norm='max',
                                            axis=1)
Exemplo n.º 30
0
    "topK": 466,
    "shrink": 9,
    "similarity": "dice",
    "normalize": False
}

URM_train = sps.csr_matrix(URM_train)
profile_length = np.ediff1d(URM_train.indptr)
block_size = int(len(profile_length) * 0.05)
sorted_users = np.argsort(profile_length)

slim_model = SLIM_BPR_Cython(URM_train, recompile_cython=False)
slim_model.fit(**slim_best_parameters)
rp3_model = RP3betaRecommender(URM_train)
rp3_model.fit(**rp3_best_parameters)
userCF_model = UserKNNCFRecommender(URM_train)
userCF_model.fit(**userKNNCF_best_parameters)

MAP_slim_per_group = []
MAP_rp3_per_group = []
MAP_userCF_per_group = []
cutoff = 10

URM_train = sps.csr_matrix(URM_train)
profile_length = np.ediff1d(URM_train.indptr)
block_size = int(len(profile_length) * 0.05)
sorted_users = np.argsort(profile_length)

for group_id in range(0, 20):
    start_pos = group_id * block_size
    end_pos = min((group_id + 1) * block_size, len(profile_length))