示例#1
0
class ColdUsersTopPopRecommender(BaseItemSimilarityMatrixRecommender):

    RECOMMENDER_NAME = "ColdUsersTopPopRecommender"

    def __init__(self, URM_train, Recommender):
        super(ColdUsersTopPopRecommender, self).__init__(URM_train)

        self.URM_train = check_matrix(URM_train.copy(), "csr")

        self.Recommender = Recommender

        self.topPop = TopPop(URM_train)

    def fit(self):

        self.topPop.fit()

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

        item_weights = self.Recommender._compute_item_score(
            user_id_array, items_to_compute)

        for index, user_id in enumerate(user_id_array):
            if self._get_cold_user_mask()[user_id]:
                item_weights[index] += self.topPop._compute_item_score(
                    [user_id], items_to_compute)[0]

        return item_weights
示例#2
0
    def __init__(self, URM_train, Recommender):
        super(ColdUsersTopPopRecommender, self).__init__(URM_train)

        self.URM_train = check_matrix(URM_train.copy(), "csr")

        self.Recommender = Recommender

        self.topPop = TopPop(URM_train)
示例#3
0
    def __init__(self, urm_train):
        super(HybridZeroRecommender, self).__init__(urm_train)

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

        data = DataManager()

        ucm_age, ucm_region, ucm_all = data.get_ucm()

        recommender_1 = TopPop(urm_train)
        recommender_1.fit()
        #
        recommender_2 = UserKNNCBFRecommender(urm_train, ucm_all)
        recommender_2.fit(shrink=500, topK=1600, similarity='tversky')

        self.recommender_1 = recommender_1
        self.recommender_2 = recommender_2
示例#4
0
 def __init__(self,
              URM_train,
              ICM_train,
              submission=False,
              verbose=True,
              seed=1205):
     super(ColdUserEmbedding001, self).__init__(URM_train, verbose=verbose)
     self.__warm_recommender = LinearHybrid003(URM_train,
                                               ICM_train,
                                               submission=submission,
                                               verbose=verbose,
                                               seed=seed)
     self.__warm_params = {
         'alpha': 0.4357334022190923,
         'l1_ratio': 0.7855628829453338
     }
     self.__cold_recommender = TopPop(URM_train)
     self.__cold_params = {}
 def __init__(self,
              URM_train,
              ICM_train,
              submission=False,
              verbose=True,
              seed=1205):
     super(ColdUserEmbedding002, self).__init__(URM_train, verbose=verbose)
     self.__warm_recommender = LinearHybrid005(URM_train,
                                               ICM_train,
                                               submission=submission,
                                               verbose=verbose,
                                               seed=seed)
     self.__warm_params = {
         'alpha': 0.3553383791480798,
         'l1_ratio': 0.000435281815357902
     }
     self.__cold_recommender = TopPop(URM_train)
     self.__cold_params = {}
示例#6
0
 def __init__(self, data: DataObject):
     super(Hybrid100AlphaRecommender, self).__init__(data.urm_train)
     self.data = data
     self.rec1 = UserKNNCBFRecommender(data.ucm_all, data.urm_train)
     self.rec2 = TopPop(data.urm_train)
     self.rec1.fit(topK=5000,
                   shrink=5,
                   feature_weighting="TF-IDF",
                   similarity="euclidean")
     self.rec2.fit()
     cold = data.ids_cold_user
     train_cold = data.urm_train_users_by_type[0][1]
     if train_cold.shape[0] > 0:
         target_users = np.append(cold, train_cold)
     else:
         target_users = cold
     self.target_users = target_users
     self.hybrid_rec = Hybrid1CXAlphaRecommender(
         data,
         recommenders=[self.rec1, self.rec2],
         recommended_users=target_users,
         max_cutoff=20)
示例#7
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
示例#8
0
class ColdUserEmbedding001(BaseRecommender):
    RECOMMENDER_NAME = "ColdUserEmbedding001"

    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(ColdUserEmbedding001, self).__init__(URM_train, verbose=verbose)
        self.__warm_recommender = LinearHybrid003(URM_train,
                                                  ICM_train,
                                                  submission=submission,
                                                  verbose=verbose,
                                                  seed=seed)
        self.__warm_params = {
            'alpha': 0.4357334022190923,
            'l1_ratio': 0.7855628829453338
        }
        self.__cold_recommender = TopPop(URM_train)
        self.__cold_params = {}

    def fit(self):
        self.__cold_recommender.fit()
        self.__warm_recommender.fit(**self.__warm_params)

    def recommend(self,
                  user_id_array,
                  cutoff=None,
                  remove_seen_flag=True,
                  items_to_compute=None,
                  remove_top_pop_flag=False,
                  remove_custom_items_flag=False,
                  return_scores=False):
        # If is a scalar transform it in a 1-cell array
        if np.isscalar(user_id_array):
            user_id_array = np.atleast_1d(user_id_array)
            single_user = True
        else:
            single_user = False

        if cutoff is None:
            cutoff = self.URM_train.shape[1] - 1

        ranking_list = []
        for user_id in user_id_array:
            user_profile_length = len(self.URM_train[user_id].data)
            recommender = self.__get_recommender_by_profile_length(
                user_profile_length)
            ranking_list.append(
                recommender.recommend(
                    user_id,
                    cutoff=cutoff,
                    remove_seen_flag=remove_seen_flag,
                    items_to_compute=items_to_compute,
                    remove_top_pop_flag=remove_top_pop_flag,
                    remove_custom_items_flag=remove_custom_items_flag,
                    return_scores=False))
        # Return single list for one user, instead of list of lists
        if single_user:
            ranking_list = ranking_list[0]
        return ranking_list

    def _compute_item_score(self, user_id_array, items_to_compute=None):
        raise NotImplementedError(
            f"{self.RECOMMENDER_NAME}: compute_item_score not assigned for current recommender, unable to compute "
            f"prediction scores")

    def save_model(self, folder_path, file_name=None):
        raise NotImplementedError(
            f"{self.RECOMMENDER_NAME}: save_model not assigned for current recommender, unable to save the model"
        )

    def __get_recommender_by_profile_length(self, user_profile_length):
        if user_profile_length < 1:
            return self.__cold_recommender
        return self.__warm_recommender
示例#9
0
URM_train, URM_validation, URM_test = dataSplitter.get_holdout_split()

ICM_dict = dataSplitter.get_loaded_ICM_dict()

# Now that we have the split, we can create the evaluators.
# The constructor of the evaluator allows you to specify the evaluation conditions (data, recommendation list length,
# excluding already seen items). Whenever you want to evaluate a model, use the evaluateRecommender function of the evaluator object
evaluator_validation = EvaluatorHoldout(URM_validation,
                                        cutoff_list=[5],
                                        exclude_seen=False)
evaluator_test = EvaluatorHoldout(URM_test,
                                  cutoff_list=[5, 10, 20],
                                  exclude_seen=False)

# We now fit and evaluate a non personalized algorithm
recommender = TopPop(URM_train)
recommender.fit()

results_dict, results_run_string = evaluator_validation.evaluateRecommender(
    recommender)
print("Result of TopPop is:\n" + results_run_string)

# We now fit and evaluate a personalized algorithm passing some hyperparameters to the fit functions
recommender = P3alphaRecommender(URM_train)
recommender.fit(topK=100, alpha=0.5)

results_dict, results_run_string = evaluator_validation.evaluateRecommender(
    recommender)
print("Result of P3alpha is:\n" + results_run_string)

# We now use a content-based algorithm and a hybrid content-collaborative algorithm
示例#10
0
class Hybrid100AlphaRecommender(BaseRecommender):
    """Hybrid100AlphaRecommender recommender"""

    RECOMMENDER_NAME = "Hybrid100AlphaRecommender"

    def __init__(self, data: DataObject):
        super(Hybrid100AlphaRecommender, self).__init__(data.urm_train)
        self.data = data
        self.rec1 = UserKNNCBFRecommender(data.ucm_all, data.urm_train)
        self.rec2 = TopPop(data.urm_train)
        self.rec1.fit(topK=5000,
                      shrink=5,
                      feature_weighting="TF-IDF",
                      similarity="euclidean")
        self.rec2.fit()
        cold = data.ids_cold_user
        train_cold = data.urm_train_users_by_type[0][1]
        if train_cold.shape[0] > 0:
            target_users = np.append(cold, train_cold)
        else:
            target_users = cold
        self.target_users = target_users
        self.hybrid_rec = Hybrid1CXAlphaRecommender(
            data,
            recommenders=[self.rec1, self.rec2],
            recommended_users=target_users,
            max_cutoff=20)

    def fit(self):
        weights = [
            [
                1.0, 0.6821568252468901, 0.6849024969656253,
                0.5278884115166208, 0.5256279588345396, 0.3523944402333277,
                0.35917072524518867, 0.3781483902477164, 0.35494012640675526,
                0.2569761057765945, 0.2686708563615972, 0.26047695765786827,
                0.18963922521929752, 0.19796884202620682, 0.21062192323921447,
                0.21086230330374983, 0.20645211181144751, 0.2051545899987307,
                0.195630375727827, 0.16433006044601312, 0.15898416104687418,
                0.13710408424057147, 0.14535409877804073, 0.14093706131152708,
                0.13925057012140438, 0.10897116615151631, 0.08690310920352357,
                0.05452137761633189, 0.039626490480693964, 0.038884188246590415
            ],
            [
                0.12184895015208339, 0.10412792995227525, 0.11245816434845729,
                0.09237425432552643, 0.07790937226904884, 0.08414212205057275,
                0.06899821248751226, 0.07451806948651324, 0.06080223143736178,
                0.05923095707738565, 0.0639694336435765, 0.06890447942434409,
                0.06474046677091937, 0.0672866612162612, 0.06627995889505314,
                0.06170959010489348, 0.05576908741213118, 0.05516680106058056,
                0.04913346628366165, 0.042346032068023606, 0.04268540340456829,
                0.03738588109631485, 0.03801703741074155, 0.03634930102905383,
                0.027148968808256298, 0.023744038897221154,
                0.019516056544059187, 0.009815556133702266,
                0.0038914891192522562, 0.0034155098002848494
            ]
        ]
        self.hybrid_rec.weights = weights

    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 []
示例#11
0
print("The MAP in one test is: ", MAP)

itemCF_recommender = ItemKNNCFRecommender(URM_all)
itemCF_recommender.fit(**itemCFParam)
slim_recommender = SLIM_BPR_Cython(URM_all, recompile_cython=False)
slim_recommender.fit(**slimParam)
p3_recommender = P3alphaRecommender(URM_all)
p3_recommender.fit(**p3Param)
recommender1 = SimilarityHybridRecommender(URM_all,
                                           itemCF_recommender.W_sparse,
                                           slim_recommender.W_sparse,
                                           p3_recommender.W_sparse)
recommender1.fit(topK=100, alpha1=alpha1, alpha2=alpha2, alpha3=alpha3)
recommender1.save_model("model/", "hybrid_item_slim_basic")

topPopRecommender = TopPop(URM_all)
topPopRecommender.fit()

results_test = test_save(recommender1, topPopRecommender, test_path,
                         warm_users)
create_csv(results_test, 'Cold_Item_SLIM_BPR_parameter_tuning')
print("finished")
print("The MAP in one test is: ", MAP)
print("The average best MAP is: ", average_map)
"""
mapLists = np.zeros((len(userCFParamList), len(slimParamList), len(p3ParamList)))
num_iteration = 2

for _ in range(num_iteration):
    for i, user_cf_param in enumerate(userCFParamList):
        for j, slim_param in enumerate(slimParamList):