class Hybrid001AlphaRecommender(BaseRecommender): """Hybrid001AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid001AlphaRecommender" def __init__(self, URM_train, UCM, cold_users, warm_users): super(Hybrid001AlphaRecommender, self).__init__(URM_train) self.warm_recommender = ItemKNNCFRecommender(URM_train) self.cold_recommender = UserKNNCBFRecommender(UCM, URM_train) self.cold_users = cold_users self.warm_users = warm_users def fit(self): self.warm_recommender.fit(topK=12, shrink=16) self.cold_recommender.fit(topK=11000, shrink=2) 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)
def fit(self): self.cold_recommender.fit() rec_augmenter_cold_item = ItemKNNCBFOnlyColdRecommender(self.data) # r2 = ItemKNNCBFRecommender(data.urm_train, data.icm_all_augmented) rec_augmenter_cold_item.fit(topK=10, shrink=1, feature_weighting="BM25") self.warm_recommender.augment(rec_augmenter_cold_item, self.data.ids_warm_train_users, self.warm_recommender.augmented_matrix, cutoff=3, amount=1) rec_warm = RP3betaRecommender(self.warm_recommender.augmented_matrix) rec_warm.fit(topK=20, alpha=0.16, beta=0.24) self.warm_recommender.fit(rec_warm) self.warm_1_recommender.fit() self.warm_7_recommender.augment( rec_augmenter_cold_item, self.data.ids_warm_train_users, self.warm_7_recommender.augmented_matrix, cutoff=3, amount=1) rec_warm_7 = ItemKNNCFRecommender( self.warm_7_recommender.augmented_matrix) rec_warm_7.fit(topK=22, shrink=850, feature_weighting="BM25", similarity="jaccard") self.warm_7_recommender.fit(rec_warm_7)
def __init__(self, URM_train, verbose=True): super(MergedHybrid000, self).__init__(URM_train, verbose=verbose) rec1 = RP3betaRecommender(URM_train, verbose=False) try: rec1.load_model( 'stored_recommenders/RP3betaRecommender/best_at_26_10_20') except: rec1.fit(alpha=0.4530815441932864, beta=0.008742088319964482, topK=104, normalize_similarity=False) rec1.save_model( 'stored_recommenders/RP3betaRecommender/best_at_26_10_20') rec2 = ItemKNNCFRecommender(URM_train, verbose=False) try: rec2.load_model( 'stored_recommenders/ItemKNNCFRecommender/best_at_26_10_20') except: rec2.fit(topK=967, shrink=356, similarity='cosine', normalize=True) rec2.save_model( 'stored_recommenders/ItemKNNCFRecommender/best_at_26_10_20') self.rec1 = rec1 self.rec2 = rec2 self.rec1_W_sparse = rec1.W_sparse.copy() self.rec2_W_sparse = rec2.W_sparse.copy() self.URM_train = URM_train
class Hybrid105AlphaRecommender(BaseRecommender): """Hybrid105AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid105AlphaRecommender" def __init__(self, data: DataObject): super(Hybrid105AlphaRecommender, self).__init__(data.urm_train) self.data = data # self.rec1 = SLIM_BPR_Cython(data.urm_train) self.rec2 = ItemKNNCFRecommender(data.urm_train) self.rec3 = RP3betaRecommender(data.urm_train) # self.rec1.fit(sgd_mode="adagrad", topK=50, epochs=150, learning_rate=1e-05, lambda_i=1, lambda_j=0.001) self.rec2.fit(topK=10, shrink=30, similarity="tanimoto") self.rec3.fit(topK=30, alpha=0.24, beta=0.24, implicit=True, normalize_similarity=True) self.hybrid_rec = Hybrid1XXAlphaRecommender(data, recommenders=[self.rec2, self.rec3], max_cutoff=20) def fit(self): weights = np.array([[91.45, 46.9, 36.6, 26.75, 19.85, 18.45, 14.95, 11.9, 14.25, 9.9, 10.65, 10.65, 9.85, 9.2, 7.6, 7.8, 6.6, 7.05, 5.8, 6.25, 4.7, 5., 5.45, 4.45, 3.8, 4.05, 4.05, 4.05, 3.75, 3.95], [94.7, 51.2, 34.25, 26.45, 22.25, 17.55, 15.7, 13.7, 10.75, 9.9, 9.15, 8.2, 6.6, 6.35, 6.65, 5.45, 5.25, 4.4, 5.6, 3.95, 3.2, 3.85, 2.6, 2.85, 2.75, 2.3, 2.55, 2.4, 2.9, 1.6]]) weights[0] = weights[0] / 3 * 2 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)
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
def objective(**params): recommender = ItemKNNCFRecommender(URM_train) recommender.fit(**params) print(f"RUN#{run}:\n> space: [ topK={recommender.topK}, shrink={recommender.shrink} ]") result_dict, _ = evaluator_validation.evaluateRecommender(recommender) MAP = result_dict[10]['MAP'] print(f"> result: [ MAP={MAP} ]") return -MAP
class Hybrid3ScoreSubRecommender(BaseRecommender): """Hybrid3ScoreSubRecommender recommender""" RECOMMENDER_NAME = "Hybrid3ScoreRecommender" def __init__(self, data: DataObject): super(Hybrid3ScoreSubRecommender, self).__init__(data.urm_train) urm = sps.vstack([data.urm_train, data.icm_all_augmented.T]) urm = urm.tocsr() self.slim = SLIMElasticNetRecommender(urm) self.rp3 = RP3betaRecommender(self.URM_train) self.itemcf = ItemKNNCFRecommender(self.URM_train) self.random_seed = data.random_seed def fit(self, alpha_beta_ratio=1, alpha_gamma_ratio=1): try: self.slim.load_model('stored_recommenders/slim_elastic_net/', f'with_icm_{self.random_seed}_topK=100_l1_ratio=0.04705_alpha=0.00115_positive_only=True_max_iter=35') except: self.slim.fit(topK=100, l1_ratio=0.04705, alpha=0.00115, positive_only=True, max_iter=35) self.slim.save_model('stored_recommenders/slim_elastic_net/', f'with_icm_{self.random_seed}_topK=100_l1_ratio=0.04705_alpha=0.00115_positive_only=True_max_iter=35') self.rp3.fit(topK=20, alpha=0.16, beta=0.24) self.itemcf.fit(topK=22, shrink=850, similarity='jaccard', feature_weighting='BM25') self.alpha = 1 self.beta = alpha_beta_ratio self.gamma = alpha_gamma_ratio def _compute_item_score(self, user_id_array, items_to_compute = None): # ATTENTION! # THIS METHOD WORKS ONLY IF user_id_array IS A SCALAR AND NOT AN ARRAY # TODO scores_slim = self.slim._compute_item_score(user_id_array=user_id_array) scores_rp3 = self.rp3._compute_item_score(user_id_array=user_id_array) scores_itemcf = self.itemcf._compute_item_score(user_id_array=user_id_array) # normalization slim_max = scores_slim.max() rp3_max = scores_rp3.max() itemcf_max = scores_itemcf.max() if not slim_max == 0: scores_slim /= slim_max if not rp3_max == 0: scores_rp3 /= rp3_max if not itemcf_max == 0: scores_itemcf /= itemcf_max scores_total = self.alpha * scores_slim + self.beta * scores_rp3 + self.gamma * scores_itemcf return scores_total
class Hybrid3ScoreRecommender(BaseRecommender): """Hybrid3ScoreRecommender recommender""" RECOMMENDER_NAME = "Hybrid3ScoreRecommender" def __init__(self, data: DataObject, random_seed: int, alpha=1): super(Hybrid3ScoreRecommender, self).__init__(data.urm_train) self.random_seed = random_seed self.slim = SLIMElasticNetRecommender(self.URM_train) self.rp3 = RP3betaRecommender(self.URM_train) self.itemcf = ItemKNNCFRecommender(self.URM_train) self.alpha = alpha def fit(self, alpha_beta_ratio=1, alpha_gamma_ratio=1): self.slim.load_model( '', 'SLIM_ElasticNetURM_seed=' + str(self.random_seed) + '_topK=100_l1_ratio=0.04705_alpha=0.00115_positive_only=True_max_iter=35' ) self.rp3.fit(topK=20, alpha=0.16, beta=0.24) self.itemcf.fit(topK=22, shrink=850, similarity='jaccard', feature_weighting='BM25') # self.alpha = 1 self.beta = self.alpha * alpha_beta_ratio self.gamma = self.alpha * alpha_gamma_ratio def _compute_item_score(self, user_id_array, items_to_compute=None): # ATTENTION! # THIS METHOD WORKS ONLY IF user_id_array IS A SCALAR AND NOT AN ARRAY # TODO scores_slim = self.slim._compute_item_score( user_id_array=user_id_array) scores_rp3 = self.rp3._compute_item_score(user_id_array=user_id_array) scores_itemcf = self.itemcf._compute_item_score( user_id_array=user_id_array) # normalization slim_max = scores_slim.max() rp3_max = scores_rp3.max() itemcf_max = scores_itemcf.max() if not slim_max == 0: scores_slim /= slim_max if not rp3_max == 0: scores_rp3 /= rp3_max if not itemcf_max == 0: scores_itemcf /= itemcf_max scores_total = self.alpha * scores_slim + self.beta * scores_rp3 + self.gamma * scores_itemcf return scores_total
class Hybrid101AlphaRecommender(BaseRecommender): """Hybrid101AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid101AlphaRecommender" def __init__(self, data: DataObject): super(Hybrid101AlphaRecommender, self).__init__(data.urm_train) self.data = data self.rec1 = SLIM_BPR_Cython(data.urm_train) self.rec2 = ItemKNNCFRecommender(data.urm_train) self.rec3 = RP3betaRecommender(data.urm_train) self.rec1.fit(sgd_mode="adagrad", topK=15000, epochs=250, learning_rate=1e-05, lambda_i=0.01, lambda_j=0.01) # self.rec1.fit(topK=435, epochs=115, symmetric=True, sgd_mode='adagrad', lambda_i=0.0010067865845523253, # lambda_j=3.764224446055186e-05, learning_rate=0.00024125117955549121) self.rec2.fit(topK=20000, shrink=20000, feature_weighting="TF-IDF") self.rec3.fit(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 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 eval_one_data(random_seed): data_reader = DataReader() data = DataObject(data_reader, 1, random_seed=random_seed) result = [] recommender = ItemKNNCFRecommender(data.urm_train) recommender.fit(topK = args["topK"], shrink=args["shrink"], similarity=args["similarity"], feature_weighting=args["feature_weighting"]) for n, users, description in data.urm_train_users_by_type: eval, map = MyEvaluator.evaluate_algorithm(data.urm_test, users, recommender, at=10, remove_top=0) result.append(map) users = data.ids_target_users eval, map = MyEvaluator.evaluate_algorithm(data.urm_test, users, recommender, at=10, remove_top=0) result.append(map) return result
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)
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
def __init__(self, URM_train): super(HybridWarmRecommender, self).__init__(URM_train) recommender_1 = ItemKNNCFRecommender(URM_train) recommender_1.fit(topK=20, shrink=30) recommender_2 = SLIM_BPR_Cython(URM_train) recommender_2.fit(epochs=40, lambda_i=0.01, lambda_j=0.001, learning_rate=1e-4, topK=200) # recommender_3 = TopPop(URM_train) # recommender_3.fit() self.URM_train = check_matrix(URM_train.copy(), 'csr') self.recommender_1 = recommender_1 self.recommender_2 = recommender_2
def split_and_fit(self, random_seed): print(random_seed) data = self.data # Split policy new_urm_train = split_with_triple(data.augmented_urm, self.split_policy)[0] rec = ItemKNNCFRecommender(new_urm_train) rec.fit( topK=self.topK, shrink=self.shrink, similarity=self.similarity, feature_weighting=self.feature_weighting ) rec.URM_train = data.urm_train return rec
class Hybrid111AlphaRecommender(BaseRecommender): """Hybrid111AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid111AlphaRecommender" def __init__(self, data: DataObject): super(Hybrid111AlphaRecommender, self).__init__(data.urm_train) self.data = data self.rec1 = RP3betaRecommender(data.urm_train) self.rec2 = ItemKNNCFRecommender(data.urm_train) self.rec1.fit(topK=20, alpha=0.11, beta=0.18) self.rec2.fit(topK=18, shrink=850, similarity='jaccard', feature_weighting='BM25') cold = data.ids_cold_user self.target_users = data.urm_train_users_by_type[11][1] self.hybrid_rec = Hybrid1CXAlphaRecommender(data, recommenders=[self.rec1, self.rec2], recommended_users=self.target_users, max_cutoff=30) def fit(self): weights = [ [0.36263544103305234, 0.2821869186577171, 0.2958040731688242, 0.29923612866193294, 0.25220249762628333, 0.2761756648779422, 0.18915192083976606, 0.182117148228788, 0.1842783740517236, 0.1486885469397763, 0.15841860217081513, 0.1313525617771801, 0.1495204046677669, 0.1442400061423464, 0.14584743502512043, 0.12150982415065534, 0.12535649682927805, 0.08906867097805428, 0.1012469101805646, 0.08566556941254282, 0.08865930444526858, 0.0910510345404526, 0.06557183202757007, 0.05584782961979419, 0.05557650728442622, 0.030967327271124927, 0.029196638757279396, 0.025490236322759728, 0.01569027879218803, 0.015055828167915425], [1.0, 0.5214104256707622, 0.20746796675006374, 0.23560220633621676, 0.2575770382835032, 0.2512775337476986, 0.28896916380985344, 0.3287565885723311, 0.19352951261468151, 0.22255893950688374, 0.12074099390243273, 0.13073528889178715, 0.13251088387516363, 0.13834537392481033, 0.10694898922318279, 0.1211777917594245, 0.1276279143114306, 0.14661389007809034, 0.14142761528430872, 0.11969443714497587, 0.13398226206599023, 0.14707641785555184, 0.11689418050769908, 0.0471984606450728, 0.049494500313232624, 0.04680805679550986, 0.042358887859941775, 0.04342842109287542, 0.0, 0.0]] 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 []
class Hybrid104AlphaRecommender(BaseRecommender): """Hybrid104AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid104AlphaRecommender" def __init__(self, data: DataObject): super(Hybrid104AlphaRecommender, self).__init__(data.urm_train) self.data = data self.rec1 = RP3betaRecommender(data.urm_train) self.rec1.fit(topK=26, alpha=0.25, beta=0.21) self.rec2 = ItemKNNCFRecommender(data.urm_train) self.rec2.fit(topK=10, shrink=1000, similarity="tanimoto", feature_weighting="BM25") target_users = data.urm_train_users_by_type[4][1] self.target_users = target_users self.hybrid_rec = Hybrid1CXAlphaRecommender(data, recommenders=[self.rec1, self.rec2], recommended_users=target_users, max_cutoff=30) def fit(self): weights1 = [ [1.0, 0.4517179068981683, 0.3979690096061162, 0.4068926692801725, 0.3404408707736778, 0.35438712937353284, 0.3107497990263178, 0.277027821536975, 0.22460270448263392, 0.258293110155029, 0.22565957216308793, 0.1427039025093434, 0.14850612508172897, 0.1427281482083753, 0.1513486708877009, 0.14931902151854687, 0.1717168747463289, 0.0783259533439393, 0.08066358870496233, 0.0799579856421651, 0.06310732697276022, 0.0679217517962877, 0.07689888001915678, 0.06585136121968413, 0.05989375609327328, 0.05668195370281998, 0.06377071839974079, 0.017091307253589913, 0.017276230828121276, 0.012865982715760244], [0.38688356604557783, 0.3653660032353448, 0.30862315365153425, 0.21699342252527684, 0.24954243590406833, 0.204684429683601, 0.11665613023016438, 0.12839454425138505, 0.14122535566771405, 0.1504381157229052, 0.17300383308134096, 0.1309799007170742, 0.1354656505605249, 0.08481274450716023, 0.09302787057145857, 0.09643918642755862, 0.10731710201865315, 0.07003981095966517, 0.06217937411084906, 0.0457095602617101, 0.04370382323931054, 0.050259396725207114, 0.05401448061464968, 0.03742468372661131, 0.038730399329483335, 0.04401751666811602, 0.01793705784582081, 0.020478791947760068, 0.010738842299817633, 0.0] ] 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 []
class Hybrid109AlphaRecommender(BaseRecommender): """Hybrid109AlphaRecommender recommender""" RECOMMENDER_NAME = "Hybrid109AlphaRecommender" def __init__(self, data: DataObject): super(Hybrid109AlphaRecommender, self).__init__(data.urm_train) self.data = data self.rec1 = RP3betaRecommender(data.urm_train) self.rec2 = ItemKNNCFRecommender(data.urm_train) self.rec1.fit(topK=20, alpha=0.11, beta=0.18) self.rec2.fit(topK=18, shrink=850, similarity='jaccard', feature_weighting='BM25') cold = data.ids_cold_user self.target_users = data.urm_train_users_by_type[10][1] self.hybrid_rec = Hybrid1CXAlphaRecommender(data, recommenders=[self.rec1, self.rec2], recommended_users=self.target_users, max_cutoff=30) def fit(self): weights = [ [1.0, 0.7342122774376785, 0.34497645278822586, 0.36927385971216775, 0.2656415434217909, 0.1919421372364918, 0.13293725674498652, 0.14834334445233766, 0.09775023936714271, 0.10937034954552564, 0.12423098623036653, 0.12128788329523746, 0.09922803890669518, 0.11319974533876793, 0.12225324687713784, 0.12652916395766228, 0.1455085385513116, 0.0914697268325847, 0.0864212005177428, 0.0927393080791469, 0.07406451741135928, 0.07317443382010559, 0.05931225834380707, 0.0648359502039145, 0.048739304754641055, 0.03240929388889551, 0.03666804622590347, 0.04216825315978899, 0.0447766477977417, 0.0], [0.4408648438266093, 0.4922839735436428, 0.24278674016874366, 0.2625681515299236, 0.20164121702221108, 0.1910052083663528, 0.16326625410923354, 0.0923349547002018, 0.0945701088881237, 0.10221568403081797, 0.11754803663544067, 0.1301553725839876, 0.11021196426884693, 0.11344510775804244, 0.11457061547288103, 0.08422114145406502, 0.09685431267217476, 0.10379217500012262, 0.10710455074654938, 0.07314029501013618, 0.08298821173426496, 0.09228327804768503, 0.07716595603811922, 0.060006078819213785, 0.06542915168076886, 0.05415221757898212, 0.03694072713955989, 0.028810312392733607, 0.03269610716181971, 0.0]] 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 []
# 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, w_cbart=0.3, w_cbalb=0.6,
'alpha': 0.5626527178823623, 'min_rating': 0.4999280105627021, 'implicit': [False, False, False] } alpha1 = 0.4 alpha2 = 0.5399999999999999 alpha3 = 0.06000000000000005 print( "***************************Ensure the parameter is good**********************" ) URM_train, URM_test = train_test_holdout(URM_all, train_perc=0.8) itemCF_recommender = ItemKNNCFRecommender(URM_train) itemCF_recommender.fit(**itemCFParam) slim_recommender = SLIM_BPR_Cython(URM_train, recompile_cython=False) slim_recommender.fit(**slimParam) p3_recommender = P3alphaRecommender(URM_train) p3_recommender.fit(**p3Param) recommender1 = SimilarityHybridRecommender(URM_train, itemCF_recommender.W_sparse, slim_recommender.W_sparse, p3_recommender.W_sparse) recommender1.fit(topK=100, alpha1=alpha1, alpha2=alpha2, alpha3=alpha3) evaluator_validation = EvaluatorHoldout(URM_test, cutoff_list=[10]) eval_res = evaluator_validation.evaluateRecommender(recommender1) MAP = eval_res[0][10]['MAP'] print("The MAP in one test is: ", MAP)
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)
URM_all = sps.load_npz('myFiles/URM_all.npz') URM_train_temp, URM_test = splitURM(URM_all) URM_train, URM_validation = splitURM(URM_train_temp) URM_train = URM_train.tocsr() URM_test = URM_test.tocsr() URM_validation = URM_validation.tocsr() ICM_all = sps.load_npz('myFiles/ICM_all.npz') evaluator_validation = EvaluatorHoldout(URM_validation, cutoff_list=[10]) evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[10]) itemKNNCF = ItemKNNCFRecommender(URM_train) itemKNNCF.fit(shrink=24, topK=10) recommenderCYTHON = SLIM_BPR_Cython(URM_train, recompile_cython=False) recommenderCYTHON.fit(epochs=2000, batch_size=200, sgd_mode='sdg', learning_rate=1e-5, topK=10) recommenderCB = ItemKNNCBFRecommender(URM_train, ICM_all) recommenderCB.fit(shrink=24, topK=10) 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')
from Base.Evaluation.Evaluator import SequentialEvaluator evaluator = SequentialEvaluator(URM_test, URM_train, exclude_seen=True) output_root_path = "result_experiments/" # If directory does not exist, create if not os.path.exists(output_root_path): os.makedirs(output_root_path) logFile = open(output_root_path + "result_all_algorithms.txt", "a") transfer_learning = False if transfer_learning: recommender_IB = ItemKNNCFRecommender(URM_train) recommender_IB.fit(200, 15) transfer_matrix = recommender_IB.W_sparse else: transfer_matrix = None try: recommender_class = HybridRecommender print("Algorithm: {}".format(recommender_class)) ''' Our optimal run ''' recommender_list = recommender_list1 + recommender_list2 # + recommender_list3 onPop = True
# If directory does not exist, create if not os.path.exists(output_root_path): os.makedirs(output_root_path) logFile = open(output_root_path + "result_all_algorithms.txt", "a") for recommender_class in recommender_list: try: print("Algorithm: {}".format(recommender_class)) recommender = recommender_class(URM_train) item = ItemKNNCFRecommender(URM_train) W_sparse_CF = item.fit(topK=800, shrink=8, similarity='cosine', normalize=True) CFW_weithing = CFW_D_Similarity_Linalg(URM_train, ICM_Art, W_sparse_CF) CFW_weithing.fit(topK=8, loss_tolerance=1e-04, add_zeros_quota=0) recommender.fit(CFW=CFW_weithing) results_run, results_run_string = evaluator.evaluateRecommender( recommender) print("Algorithm: {}, results: \n{}".format( recommender.__class__, results_run_string)) logFile.write("Algorithm: {}, results: \n{}\n".format( recommender.__class__, results_run_string)) logFile.flush() except Exception as e: traceback.print_exc()
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("-------------------") # print("---PureSVD fitted--") # print("-------------------") #hybridRecommender = ItemKNNSimilarityHybridRecommender(URM_train, ItemKNNCF.W_sparse, ContentBased.W_sparse) hybridRecommender_scores = ItemKNNScoresHybridRecommender_multiple(URM_train, ItemKNNCF, ContentBased, UserBased) #hybridRecommender_scores = ItemKNNScoresHybridRecommender(URM_train, ItemKNNCF, PureSVD) alpha_list = np.arange(0.1, 1.0, 0.1) l1 = (alpha_list)
weight_list = list(itertools.product(l1, l1, l1)) only_sum_equal_1 = list(filter(lambda x: sum(list(x)) == 1, weight_list)) only_sum_equal_1 = list(map(lambda x: list(x), only_sum_equal_1)) # ------------------------------------------------------------- # Fitting recommender: # # The hybrid recommender constructor needs fitted recommenders # ------------------------------------------------------------- UserBased = UserKNNCFRecommender(URM_train) ItemBased = ItemKNNCFRecommender(URM_train) ContentBased = ItemKNNCBFRecommender(ICM, URM_train) UserBased.fit(topK=500, shrink=10) ItemBased.fit(topK=400, shrink=50) ContentBased.fit(topK=5, shrink=1000) hybrid = ItemKNNScoresHybridRecommender_multiple_grid(URM_train, ItemBased, ContentBased, UserBased) # ------------------------------- # Set path and file # ------------------------------- output_root_path = "result_experiments/" # If directory does not exist, create if not os.path.exists(output_root_path): os.makedirs(output_root_path) logFile = open(output_root_path + "Hybrid_GridSearch.txt", "a")
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 LinearHybridC001(BaseItemSimilarityMatrixRecommender): RECOMMENDER_NAME = "LinearHybridC001" """ This hybrid works for users who have a profile length shorter or equal to 2 interactions """ # 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(LinearHybridC001, self).__init__(URM_train, verbose=verbose) self.URM_train = URM_train self.ICM_train = ICM_train # seed 1205: {'num_factors': 83, 'confidence_scaling': 'linear', 'alpha': 28.4278070726612, 'epsilon': # 1.0234211788885077, 'reg': 0.0027328110246575004, 'epochs': 20} self.__rec1 = IALSRecommender(URM_train, verbose=False) self.__rec1_params = { 'num_factors': 83, 'confidence_scaling': 'linear', 'alpha': 28.4278070726612, 'epsilon': 1.0234211788885077, 'reg': 0.0027328110246575004, 'epochs': 15 } #### -5!! # seed 1205: {'topK': 225, 'shrink': 1000, 'similarity': 'cosine', 'normalize': True, 'feature_weighting': # 'BM25'} self.__rec2 = ItemKNNCBFRecommender(URM_train, ICM_train, verbose=False) self.__rec2_params = { 'topK': 225, 'shrink': 1000, 'similarity': 'cosine', 'normalize': True, 'feature_weighting': 'BM25' } # seed 1205: {'topK': 220, 'shrink': 175, 'similarity': 'cosine', 'normalize': False} self.__rec3 = ItemKNNCFRecommender(URM_train, verbose=False) self.__rec3_params = { 'topK': 220, 'shrink': 175, 'similarity': 'cosine', 'normalize': False } 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")
def hybrid_repo(is_test): b = Builder() ev = Evaluator() ev.split() ICM = b.build_ICM() URM_train, URM_test = train_test_holdout(b.get_URM(), train_perc=0.8) URM_train, URM_validation = train_test_holdout(URM_train, train_perc=0.9) from ParameterTuning.AbstractClassSearch import EvaluatorWrapper from Base.Evaluation.Evaluator import SequentialEvaluator evaluator_validation = SequentialEvaluator(URM_validation, cutoff_list=[5]) evaluator_test = SequentialEvaluator(URM_test, cutoff_list=[5, 10]) evaluator_validation = EvaluatorWrapper(evaluator_validation) evaluator_test = EvaluatorWrapper(evaluator_test) from KNN.ItemKNNCFRecommender import ItemKNNCFRecommender from ParameterTuning.BayesianSearch import BayesianSearch recommender_class = ItemKNNCFRecommender parameterSearch = BayesianSearch(recommender_class, evaluator_validation=evaluator_validation, evaluator_test=evaluator_test) from ParameterTuning.AbstractClassSearch import DictionaryKeys hyperparamethers_range_dictionary = {} hyperparamethers_range_dictionary["topK"] = [ 5, 10, 20, 50, 100, 150, 200, 300, 400, 500, 600, 700, 800 ] hyperparamethers_range_dictionary["shrink"] = [ 0, 10, 50, 100, 200, 300, 500, 1000 ] hyperparamethers_range_dictionary["similarity"] = ["cosine"] hyperparamethers_range_dictionary["normalize"] = [True, False] recommenderDictionary = { DictionaryKeys.CONSTRUCTOR_POSITIONAL_ARGS: [URM_train], DictionaryKeys.CONSTRUCTOR_KEYWORD_ARGS: {}, DictionaryKeys.FIT_POSITIONAL_ARGS: dict(), DictionaryKeys.FIT_KEYWORD_ARGS: dict(), DictionaryKeys.FIT_RANGE_KEYWORD_ARGS: hyperparamethers_range_dictionary } output_root_path = "result_experiments/" import os # If directory does not exist, create if not os.path.exists(output_root_path): os.makedirs(output_root_path) output_root_path += recommender_class.RECOMMENDER_NAME n_cases = 2 metric_to_optimize = "MAP" best_parameters = parameterSearch.search(recommenderDictionary, n_cases=n_cases, output_root_path=output_root_path, metric=metric_to_optimize) itemKNNCF = ItemKNNCFRecommender(URM_train) itemKNNCF.fit(**best_parameters) from FW_Similarity.CFW_D_Similarity_Linalg import CFW_D_Similarity_Linalg n_cases = 2 metric_to_optimize = "MAP" best_parameters_ItemKNNCBF = parameterSearch.search( recommenderDictionary, n_cases=n_cases, output_root_path=output_root_path, metric=metric_to_optimize) itemKNNCBF = ItemKNNCBFRecommender(ICM, URM_train) itemKNNCBF.fit(**best_parameters_ItemKNNCBF) """ #_____________________________________________________________________ from ParameterTuning.BayesianSearch import BayesianSearch from ParameterTuning.AbstractClassSearch import DictionaryKeys from ParameterTuning.AbstractClassSearch import EvaluatorWrapper evaluator_validation_tuning = EvaluatorWrapper(evaluator_validation) evaluator_test_tuning = EvaluatorWrapper(evaluator_test) recommender_class = CFW_D_Similarity_Linalg parameterSearch = BayesianSearch(recommender_class, evaluator_validation=evaluator_validation_tuning, evaluator_test=evaluator_test_tuning) hyperparamethers_range_dictionary = {} hyperparamethers_range_dictionary["topK"] = [5, 10, 20, 50, 100, 150, 200, 300, 400, 500, 600, 700, 800] hyperparamethers_range_dictionary["add_zeros_quota"] = range(0, 1) hyperparamethers_range_dictionary["normalize_similarity"] = [True, False] recommenderDictionary = {DictionaryKeys.CONSTRUCTOR_POSITIONAL_ARGS: [URM_train, ICM, itemKNNCF.W_sparse], DictionaryKeys.CONSTRUCTOR_KEYWORD_ARGS: {}, DictionaryKeys.FIT_POSITIONAL_ARGS: dict(), DictionaryKeys.FIT_KEYWORD_ARGS: dict(), DictionaryKeys.FIT_RANGE_KEYWORD_ARGS: hyperparamethers_range_dictionary} output_root_path = "result_experiments/" import os # If directory does not exist, create if not os.path.exists(output_root_path): os.makedirs(output_root_path) output_root_path += recommender_class.RECOMMENDER_NAME n_cases = 2 metric_to_optimize = "MAP" best_parameters_CFW_D = parameterSearch.search(recommenderDictionary, n_cases=n_cases, output_root_path=output_root_path, metric=metric_to_optimize) CFW_weithing = CFW_D_Similarity_Linalg(URM_train, ICM, itemKNNCF.W_sparse) CFW_weithing.fit(**best_parameters_CFW_D) #___________________________________________________________________________________________- """ from GraphBased.P3alphaRecommender import P3alphaRecommender P3alpha = P3alphaRecommender(URM_train) P3alpha.fit() from MatrixFactorization.PureSVD import PureSVDRecommender #pureSVD = PureSVDRecommender(URM_train) #pureSVD.fit() rec = HybridRec.HybridRec() S_UCM = b.get_S_UCM_KNN(b.get_UCM(ev.get_URM_train()), 600) S_ICM = b.build_S_ICM_knn(b.build_ICM(), 250) rec.fit(ev.get_URM_train(), ev.get_target_playlists(), ev.get_target_tracks(), ev.num_playlists_to_test, itemKNNCBF.W_sparse, itemKNNCF.W_sparse, P3alpha.W_sparse, is_test=True, alfa=0.7, avg=0.3) train_df = rec.recommend() if is_test: map5 = ev.map5(train_df) print('Hybrid MAP@10:', map5) return map5 else: print('Prediction saved!') train_df.to_csv(os.path.dirname(os.path.realpath(__file__))[:-19] + "/all/sub.csv", sep=',', index=False) return 0 #hybridrecommender = ItemKNNSimilarityHybridRecommender(URM_train, itemKNNCF.W_sparse, P3alpha.W_sparse) #hybridrecommender.fit(alpha=0.5) #print(evaluator_validation.evaluateRecommender(hybridrecommender)) """
import pandas as pd import scipy.sparse as sps from tqdm import tqdm from Algorithms import TopPopRecommender from KNN.ItemKNNCFRecommender import ItemKNNCFRecommender from printCsvFunction import print_to_csv_age_and_region data = pd.read_csv('dataset/data_train.csv' ) # reading the file data train with pandas library userList = list( data["row"]) # set of users that have a rating for at least one item itemList = list( data["col"]) # set of items that were rated by at least one user ratingList = list(data["data"]) URM_all = sps.coo_matrix((ratingList, (userList, itemList))) URM_all = URM_all.tocsr() recommenderICF = ItemKNNCFRecommender(URM_all) recommenderICF.fit(shrink=24, topK=10) recommenderTP = TopPopRecommender() recommenderTP.fit(URM_all) filename = 'out' + '59' # REMEMBER TO CHANGE THE FILE NAME!!! print_to_csv_age_and_region(recommenderICF, recommenderTP, filename)