ICM_train, ICM_test = train_test_holdout(ICM_all, train_perc=0.8) evaluator_validation = EvaluatorHoldout(URM_test, cutoff_list=[10], exclude_seen=True) earlystopping_keywargs = { "validation_every_n": 10, "stop_on_validation": True, "evaluator_object": evaluator_validation, "lower_validations_allowed": 5, "validation_metric": "MAP", } # MAP 0.057, kaggle MAP 0.054 recommender1 = SLIM_BPR_Cython(URM_train, recompile_cython=False) recommender1.load_model('SavedModels', 'SLIM_BPR_Cyrhon') #recommender1.fit(**{"topK": 865, "epochs": 1000, "symmetric": False, "sgd_mode": "adagrad", "lambda_i": 0.01, # "lambda_j": 1e-05, "learning_rate": 0.0001}) #recommender.save_model('SavedModels', 'SLIM_BPR_Cyrhon') w1 = recommender1.W_sparse # MAP 0.052 #recommender2 = P3alphaRecommender.P3alphaRecommender(URM_train) #recommender2.fit(**{"topK": 998, "alpha": 0.08643815887780361, "normalize_similarity": False}) #recommender2.save_model('SavedModels', 'P3alpha') #w2 = recommender2.W_sparse # Bad MAP 0.035 # recommender = RP3betaRecommender.RP3betaRecommender(URM_train) # recommender.fit(**{"topK": 1000, "alpha": 2.0, "beta": 0.0, "normalize_similarity": True})
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)
# sps.save_npz('URM/URM_validation.npz', URM_validation, compressed=True) # evaluator_validation = EvaluatorHoldout(URM_validation, cutoff_list=[10]) evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[5, 10]) # crea le matrici di raccomandazioni recommenderTP = TopPopRecommender() recommenderTP.fit(URM_all) itemKNNCF = ItemKNNCFRecommender(URM_all) itemKNNCF.fit(shrink=30, topK=10, similarity='jaccard', normalize=True) recommenderCYTHON = SLIM_BPR_Cython(URM_all, recompile_cython=False) # recommenderCYTHON.fit(epochs=200, batch_size=1000, sgd_mode='adagrad', learning_rate=1e-4, topK=10) # recommenderCYTHON.save_model('model/', file_name='SLIM_Cython_max') recommenderCYTHON.load_model('model/', file_name='SLIM_Cython_300_Ad') recommenderCB = ItemKNNCBFRecommender(URM_all, ICM_all_weighted) recommenderCB.fit(shrink=115, topK=10, normalize=True, similarity='jaccard') recommenderELASTIC = SLIMElasticNetRecommender(URM_all) # recommenderELASTIC.fit(topK=100, alpha=1e-4, positive_only=True, l1_ratio=0.5) # 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,
} userKNNCF_best_parameters = { "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) slim_model.load_model("./result_experiments/results_ensemble/", "slim_1") rp3_model = RP3betaRecommender(URM_train) #rp3_model.fit(**rp3_best_parameters) rp3_model.save_model("./result_experiments/results_ensemble/", "rp3_1") userCF_model = UserKNNCFRecommender(URM_train) #userCF_model.fit(**userKNNCF_best_parameters) userCF_model.load_model("./result_experiments/results_ensemble/", "userCF_1") 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)
class Hybrid110Recommender(BaseItemSimilarityMatrixRecommender): RECOMMENDER_NAME = "Hybrid110Recommender" def __init__(self, URM_train, seed: int): super(Hybrid110Recommender, self).__init__(URM_train) self.number_of_interactions_per_user = (self.URM_train > 0).sum(axis=1) self.highRange = SLIM_BPR_Cython(URM_train) self.lowRange = P3alphaRecommender(URM_train) self.midRange = RP3betaRecommender(URM_train) #self.itemcf = ItemKNNCFRecommender(urm) def fit(self): try: self.highRange.load_model("result_experiments/range_200--1/","SLIM_BPR_Recommender_best_model") except: self.highRange.fit(topK= 100, epochs= 70, symmetric= False, sgd_mode= 'adam', lambda_i= 0.01, lambda_j= 1e-05, learning_rate= 0.0001) self.lowRange.fit(topK= 685, alpha= 0.41303525095465676, normalize_similarity= False) self.midRange.fit(topK= 979, alpha= 0.42056182126095865, beta= 0.03446674275249296, normalize_similarity= False) #self.gamma = alpha_gamma_ratio 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 user_id_array in self._user_with_interactions_within(0, 50): return self.lowRange.recommend(user_id_array, cutoff=cutoff) elif user_id_array in self._user_with_interactions_within(51, 200): return self.midRange.recommend(user_id_array, cutoff=cutoff) elif user_id_array in self._user_with_interactions_over(200): return self.highRange.recommend(user_id_array, cutoff=cutoff) """recs = [] for user_id in user_id_array: if user_id in self._user_with_interactions_within(0, 200): rec = self.lowRange.recommend(user_id_array, cutoff=cutoff) elif user_id in self._user_with_interactions_over(200): rec = self.highRange.recommend(user_id_array, cutoff=cutoff) recs.append(rec) scores_batch = self._compute_item_score(user_id_array, items_to_compute=items_to_compute) return np.array(recs), scores_batch""" def _user_with_interactions_within(self, x=0, y=200): a = self.number_of_interactions_per_user x1 = np.where(a >= x) x2 = np.where(a <= y) return np.array([n for n in x1[0] if n in x2[0]]) def _user_with_interactions_over(self, x=0): a = self.number_of_interactions_per_user x1 = np.where(a > x) return np.array([n for n in x1[0]]) 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")
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")