users_in_group, invert=True) users_not_in_group = sorted_users[users_not_in_group_flag] evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[cutoff], ignore_users=users_not_in_group) results, _ = evaluator_test.evaluateRecommender(slim_model) MAP_slim_per_group.append(results[cutoff]["MAP"]) results, _ = evaluator_test.evaluateRecommender(rp3_model) MAP_rp3_per_group.append(results[cutoff]["MAP"]) results, _ = evaluator_test.evaluateRecommender(userCF_model) MAP_userCF_per_group.append(results[cutoff]["MAP"]) slim_model.save_model("./result_experiments/results_ensemble/", "slim_1") rp3_model.save_model("./result_experiments/results_ensemble/", "rp3_1") userCF_model.save_model("./result_experiments/results_ensemble/", "userCF_1") import matplotlib.pyplot as pyplot pyplot.plot(MAP_slim_per_group, label="slim") pyplot.plot(MAP_rp3_per_group, label="rp3") pyplot.plot(MAP_userCF_per_group, label="userCF") pyplot.ylabel('MAP') pyplot.xlabel('User Group') pyplot.legend() pyplot.show()
class Hybrid201AlphaRecommender(BaseRecommender): """Hybrid201AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid201AlphaRecommender" def __init__(self, data: DataObject): super(Hybrid201AlphaRecommender, self).__init__(data.urm_train) self.data = data urm = data.urm_train urm = sps.vstack([data.urm_train, data.icm_all_augmented.T]) urm = urm.tocsr() self.rec1 = SLIM_BPR_Cython(urm) self.rec2 = ItemKNNCFRecommender(urm) self.rec3 = RP3betaRecommender(urm) self.random_seed = data.random_seed try: self.rec1.load_model( "stored_recommenders/slim_bpr/", f'with_icm_{self.random_seed}_topK=15000_epochs=250_learning_rate=1e-05_lambda_i=0.01_lambda_j=0.01' ) except: self.rec1.fit(sgd_mode="adagrad", topK=15000, epochs=250, learning_rate=1e-05, lambda_i=0.01, lambda_j=0.01) self.rec1.save_model( "stored_recommenders/slim_bpr/", f'with_icm_{self.random_seed}_topK=15000_epochs=250_learning_rate=1e-05_lambda_i=0.01_lambda_j=0.01' ) try: self.rec2.load_model( "stored_recommenders/item_cf/", f'with_icm_{self.random_seed}_topK=20000_shrink=20000_feature_weighting=TF-IDF' ) except: self.rec2.fit(topK=20000, shrink=20000, feature_weighting="TF-IDF") self.rec2.save_model( "stored_recommenders/item_cf/", f'with_icm_{self.random_seed}_topK=20000_shrink=20000_feature_weighting=TF-IDF' ) try: self.rec3.load_model( "stored_recommenders/rp3_beta/", f'with_icm_{self.random_seed}_topK=10000_alpha=0.55_beta=0.01_implicit=True_normalize_similarity=True' ) except: self.rec3.fit(topK=10000, alpha=0.55, beta=0.01, implicit=True, normalize_similarity=True) self.rec3.save_model( "stored_recommenders/rp3_beta/", f'with_icm_{self.random_seed}_topK=10000_alpha=0.55_beta=0.01_implicit=True_normalize_similarity=True' ) self.hybrid_rec = Hybrid1XXAlphaRecommender( data, recommenders=[self.rec1, self.rec2, self.rec3], max_cutoff=20) def fit(self): weights = [[ 69.4, 25.7, 11.7, 9.4, 8.4, 5.4, 6.6, 6., 5.5, 5.6, 5., 4.4, 3.3, 5.7, 4.2, 3.7, 4.5, 2.8, 3.8, 3.4 ], [ 77.8, 29.3, 17.4, 9., 8.5, 8.9, 5.9, 5.9, 5.4, 5.1, 6., 6.3, 4.4, 4.6, 5.2, 4.9, 3.5, 3.3, 3.5, 4.3 ], [ 78.5, 29.2, 15.6, 10.9, 9.4, 6.5, 8.3, 5.7, 6.3, 6.6, 4.3, 4.2, 4.3, 4.6, 6.1, 4.7, 5.1, 4.7, 4.9, 5.1 ]] self.hybrid_rec.fit(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): return self.hybrid_rec.recommend(user_id_array=user_id_array, cutoff=cutoff)
class LinearHybrid006(BaseItemSimilarityMatrixRecommender): RECOMMENDER_NAME = "LinearHybrid006" # 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(LinearHybrid006, self).__init__(URM_train, verbose=verbose) self.URM_train = URM_train self.ICM_train = ICM_train self.__rec1 = P3alphaRecommender(URM_train, verbose=False) self.__rec1_params = { 'topK': 131, 'alpha': 0.33660811631883863, 'normalize_similarity': False } self.__rec2 = ItemKNNCBFRecommender(URM_train, ICM_train, verbose=False) self.__rec2_params = { 'topK': 205, 'shrink': 1000, 'similarity': 'cosine', 'normalize': True, 'feature_weighting': 'BM25' } self.__rec3 = SLIM_BPR_Cython(URM_train, verbose=False) self.__rec3_params = { 'topK': 979, 'epochs': 130, 'symmetric': False, 'sgd_mode': 'adam', 'lambda_i': 0.004947329669424629, 'lambda_j': 1.1534760845071758e-05, 'learning_rate': 0.0001 } 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")