def __init__(self, data: DataObject):
     super(Hybrid108AlphaRecommender, self).__init__(data.urm_train)
     self.data = data
     self.rec1 = ItemKNNCFRecommender(data.urm_train)
     self.rec2 = P3alphaRecommender(data.urm_train)
     self.rec3 = RP3betaRecommender(data.urm_train)
     self.rec1.fit(shrink=15, topK=12, feature_weighting="none")
     self.rec2.fit(topK=170, implicit=True, alpha=0.5)
     self.rec3.fit(topK=60, alpha=0.5, beta=0.1, implicit=True)
     self.hybrid_rec = Hybrid1XXAlphaRecommender(
         data,
         recommenders=[self.rec1, self.rec2, self.rec3],
         max_cutoff=12)
예제 #2
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearHybrid003ggg, 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 = P3alphaRecommender(URM_train, verbose=False)
        self.__rec1_params = {
            'topK': 131,
            'alpha': 0.33660811631883863,
            'normalize_similarity': False
        }

        # 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
예제 #3
0
    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
예제 #4
0
    def __init__(self,
                 URM_train,
                 ICM_train,
                 submission=False,
                 verbose=True,
                 seed=1205):
        super(LinearHybrid005, self).__init__(URM_train, verbose=verbose)
        self.URM_train = URM_train
        self.ICM_train = ICM_train

        self.__rec1 = RP3betaRecommender(URM_train, verbose=False)
        self.__rec1_params = {
            'topK': 1000,
            'alpha': 0.38192761611274967,
            'beta': 0.0,
            'normalize_similarity': False
        }

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

        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
예제 #5
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
예제 #6
0
 def __init__(self, URM_train, verbose=True):
     super(BaseItemSimilarityMatrixRecommender,
           self).__init__(URM_train, verbose=verbose)
     self.__rec1 = RP3betaRecommender(URM_train)
     self.__rec2 = P3alphaRecommender(URM_train)
     self.URM_train = URM_train
예제 #7
0
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))
    """
예제 #8
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
                 }),
        ]
예제 #9
0
print("Shape : {}".format(URM_train.__sizeof__()))

evaluator_test = SequentialEvaluator(URM_test, cutoff_list=[10])
evaluator_test = EvaluatorWrapper(evaluator_test)

output_root_path = "result_experiments/"

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)
def read_data_split_and_search():
    """
    This function provides a simple example on how to tune parameters of a given algorithm

    The BayesianSearch object will save:
        - A .txt file with all the cases explored and the recommendation quality
        - A _best_model file which contains the trained model and can be loaded with recommender.load_model()
        - A _best_parameter file which contains a dictionary with all the fit parameters, it can be passed to recommender.fit(**_best_parameter)
        - A _best_result_validation file which contains a dictionary with the results of the best solution on the validation
        - A _best_result_test file which contains a dictionary with the results, on the test set, of the best solution chosen using the validation set
    """

    seed = 1205
    parser = DataParser()

    URM_all = parser.get_URM_all()
    ICM_obj = parser.get_ICM_all()

    # SPLIT TO GET TEST PARTITION
    URM_train, URM_test = split_train_in_two_percentage_global_sample(
        URM_all, train_percentage=0.90, seed=seed)

    # SPLIT TO GET THE HYBRID VALID PARTITION
    URM_train, URM_valid_hybrid = split_train_in_two_percentage_global_sample(
        URM_train, train_percentage=0.85, seed=seed)

    # SPLIT TO GET THE sub_rec VALID PARTITION
    URM_train_bis, URM_valid_sub = split_train_in_two_percentage_global_sample(
        URM_train, train_percentage=0.85, seed=seed)

    collaborative_algorithm_list = [
        #EASE_R_Recommender
        #PipeHybrid001,
        #Random,
        #TopPop,
        #P3alphaRecommender,
        #RP3betaRecommender,
        #ItemKNNCFRecommender,
        #UserKNNCFRecommender,
        #MatrixFactorization_BPR_Cython,
        #MatrixFactorization_FunkSVD_Cython,
        #PureSVDRecommender,
        #NMFRecommender,
        #PureSVDItemRecommender
        #SLIM_BPR_Cython,
        #SLIMElasticNetRecommender
        #IALSRecommender
        #MF_MSE_PyTorch
        #MergedHybrid000
        #LinearHybrid002ggg
        HybridCombinationSearch
    ]

    content_algorithm_list = [
        #ItemKNNCBFRecommender
    ]

    from Base.Evaluation.Evaluator import EvaluatorHoldout

    evaluator_valid_sub = EvaluatorHoldout(URM_valid_sub, cutoff_list=[10])
    evaluator_valid_hybrid = EvaluatorHoldout(URM_valid_hybrid,
                                              cutoff_list=[10])
    evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[10])
    """
        # TODO: setta I GIUSTI EVALUATOR QUI!!!!
    runParameterSearch_Content_partial = partial(runParameterSearch_Content,
                                                 URM_train=URM_train,
                                                 ICM_object=ICM_obj,
                                                 ICM_name='1BookFeatures',
                                                 n_cases = 50,
                                                 n_random_starts = 20,
                                                 evaluator_validation= evaluator_valid_sub,
                                                 evaluator_test = evaluator_valid_hybrid,
                                                 metric_to_optimize = "MAP",
                                                 output_folder_path=output_folder_path,
                                                 parallelizeKNN = False,
                                                 allow_weighting = True,
                                                 #similarity_type_list = ['cosine']
                                                 )
    pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()), maxtasksperchild=1)
    pool.map(runParameterSearch_Content_partial, content_algorithm_list)
    """
    print("Rp3beta training...")
    rp3b = RP3betaRecommender(URM_train, verbose=False)
    rp3b_params = {
        'topK': 1000,
        'alpha': 0.38192761611274967,
        'beta': 0.0,
        'normalize_similarity': False
    }
    rp3b.fit(**rp3b_params)
    print("Done")
    print("P3alpha training...")
    p3a = P3alphaRecommender(URM_train, verbose=False)
    p3a_params = {
        'topK': 131,
        'alpha': 0.33660811631883863,
        'normalize_similarity': False
    }
    p3a.fit(**p3a_params)
    print("Done")
    print("ItemKnnCF training...")
    icf = ItemKNNCFRecommender(URM_train, verbose=False)
    icf_params = {
        'topK': 100,
        'shrink': 1000,
        'similarity': 'asymmetric',
        'normalize': True,
        'asymmetric_alpha': 0.0
    }
    icf.fit(**icf_params)
    print("Done")
    print("UserKnnCF training...")
    ucf = UserKNNCFRecommender(URM_train, verbose=False)
    ucf_params = {
        'topK': 190,
        'shrink': 0,
        'similarity': 'cosine',
        'normalize': True
    }
    ucf.fit(**ucf_params)
    print("Done")
    print("ItemKnnCBF training...")
    icb = ItemKNNCBFRecommender(URM_train, ICM_obj, verbose=False)
    icb_params = {
        'topK': 205,
        'shrink': 1000,
        'similarity': 'cosine',
        'normalize': True,
        'feature_weighting': 'BM25'
    }
    icb.fit(**icb_params)
    print("Done")
    print("SlimBPR training...")
    sbpr = SLIM_BPR_Cython(URM_train, verbose=False)
    sbpr_params = {
        'topK': 979,
        'epochs': 130,
        'symmetric': False,
        'sgd_mode': 'adam',
        'lambda_i': 0.004947329669424629,
        'lambda_j': 1.1534760845071758e-05,
        'learning_rate': 0.0001
    }
    sbpr.fit(**sbpr_params)
    print("Done")
    print("SlimElasticNet training...")
    sen = SLIMElasticNetRecommender(URM_train, verbose=False)
    sen_params = {
        'topK': 992,
        'l1_ratio': 0.004065081925341167,
        'alpha': 0.003725005053334143
    }
    sen.fit(**sen_params)
    print("Done")

    list_recommender = [rp3b, p3a, icf, ucf, icb, sen, sbpr]
    list_already_seen = [rp3b, p3a, icf, ucf, icb]

    for rec_perm in combinations(list_recommender, 3):

        if rec_perm not in combinations(list_already_seen, 3):

            recommender_names = '_'.join(
                [r.RECOMMENDER_NAME for r in rec_perm])
            output_folder_path = "result_experiments_v3/seed_" + str(
                seed) + '/' + recommender_names + '/'

            # If directory does not exist, create
            if not os.path.exists(output_folder_path):
                os.makedirs(output_folder_path)

            # TODO: setta I GIUSTI EVALUATOR QUI!!!!
            runParameterSearch_Collaborative_partial = partial(
                runParameterSearch_Collaborative,
                URM_train=URM_train,
                ICM_train=ICM_obj,
                metric_to_optimize="MAP",
                n_cases=50,
                n_random_starts=20,
                evaluator_validation_earlystopping=evaluator_valid_hybrid,
                evaluator_validation=evaluator_valid_hybrid,
                evaluator_test=evaluator_test,
                output_folder_path=output_folder_path,
                allow_weighting=False,
                #similarity_type_list = ["cosine", 'jaccard'],
                parallelizeKNN=False,
                list_rec=rec_perm)
            pool = multiprocessing.Pool(processes=int(
                multiprocessing.cpu_count()),
                                        maxtasksperchild=1)
            pool.map(runParameterSearch_Collaborative_partial,
                     collaborative_algorithm_list)
예제 #11
0
 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)
def read_data_split_and_search():
    """
    This function provides a simple example on how to tune parameters of a given algorithm

    The BayesianSearch object will save:
        - A .txt file with all the cases explored and the recommendation quality
        - A _best_model file which contains the trained model and can be loaded with recommender.load_model()
        - A _best_parameter file which contains a dictionary with all the fit parameters, it can be passed to recommender.fit(**_best_parameter)
        - A _best_result_validation file which contains a dictionary with the results of the best solution on the validation
        - A _best_result_test file which contains a dictionary with the results, on the test set, of the best solution chosen using the validation set
    """

    seed = 1205
    parser = DataParser()

    URM_all = parser.get_URM_all()
    ICM_obj = parser.get_ICM_all()

    # SPLIT TO GET TEST PARTITION
    URM_train, URM_test = split_train_in_two_percentage_global_sample(
        URM_all, train_percentage=0.90, seed=seed)

    # SPLIT TO GET THE HYBRID VALID PARTITION
    URM_train, URM_valid_hybrid = split_train_in_two_percentage_global_sample(
        URM_train, train_percentage=0.85, seed=seed)

    URM_valid_hybrid = parser.filter_URM_test_by_range(URM_train,
                                                       URM_valid_hybrid,
                                                       (3, -1))

    collaborative_algorithm_list = [
        # EASE_R_Recommender
        # PipeHybrid001,
        # Random,
        # TopPop,
        # P3alphaRecommender,
        # RP3betaRecommender,
        # ItemKNNCFRecommender,
        # UserKNNCFRecommender,
        # MatrixFactorization_BPR_Cython,
        # MatrixFactorization_FunkSVD_Cython,
        # PureSVDRecommender,
        # NMFRecommender,
        # PureSVDItemRecommender
        # SLIM_BPR_Cython,
        # SLIMElasticNetRecommender
        # IALSRecommender
        # MF_MSE_PyTorch
        # MergedHybrid000
        # LinearHybrid002ggg
        HybridCombinationSearch
    ]

    content_algorithm_list = [
        # ItemKNNCBFRecommender
    ]

    from Base.Evaluation.Evaluator import EvaluatorHoldout

    evaluator_valid_hybrid = EvaluatorHoldout(URM_valid_hybrid,
                                              cutoff_list=[10])
    evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[10])
    """
    earlystopping_keywargs = {"validation_every_n": 5,
                              "stop_on_validation": True,
                              "evaluator_object": evaluator_valid_hybrid,
                              "lower_validations_allowed": 5,
                              "validation_metric": 'MAP',
                              }
    
    print('IALS training...')
    ials = IALSRecommender(URM_train, verbose=False)
    ials_params = {'num_factors': 83, 'confidence_scaling': 'linear', 'alpha': 28.4278070726612,
                   'epsilon': 1.0234211788885077, 'reg': 0.0027328110246575004, 'epochs': 20}
    ials.fit(**ials_params, **earlystopping_keywargs)
    print("Done")
    
    
    print("PureSVD training...")
    psvd = PureSVDRecommender(URM_train, verbose=False)
    psvd_params = {'num_factors': 711}
    psvd.fit(**psvd_params)
    print("Done")
    """
    print("Rp3beta training...")
    rp3b = RP3betaRecommender(URM_train, verbose=False)
    rp3b_params = {
        'topK': 753,
        'alpha': 0.3873710051288722,
        'beta': 0.0,
        'normalize_similarity': False
    }
    rp3b.fit(**rp3b_params)
    print("Done")
    print("P3alpha training...")
    p3a = P3alphaRecommender(URM_train, verbose=False)
    p3a_params = {
        'topK': 438,
        'alpha': 0.41923120471415165,
        'normalize_similarity': False
    }
    p3a.fit(**p3a_params)
    print("Done")
    print("ItemKnnCF training...")
    icf = ItemKNNCFRecommender(URM_train, verbose=False)
    icf_params = {
        'topK': 565,
        'shrink': 554,
        'similarity': 'tversky',
        'normalize': True,
        'tversky_alpha': 1.9109121434662428,
        'tversky_beta': 1.7823834698905734
    }
    icf.fit(**icf_params)
    print("Done")
    print("UserKnnCF training...")
    ucf = UserKNNCFRecommender(URM_train, verbose=False)
    ucf_params = {
        'topK': 190,
        'shrink': 0,
        'similarity': 'cosine',
        'normalize': True
    }
    ucf.fit(**ucf_params)
    print("Done")
    print("ItemKnnCBF training...")
    icb = ItemKNNCBFRecommender(URM_train, ICM_obj, verbose=False)
    icb_params = {
        'topK': 205,
        'shrink': 1000,
        'similarity': 'cosine',
        'normalize': True,
        'feature_weighting': 'BM25'
    }
    icb.fit(**icb_params)
    print("Done")
    """
    print("SlimElasticNet training...")
    sen = SLIMElasticNetRecommender(URM_train, verbose=False)
    sen_params = {'topK': 954, 'l1_ratio': 3.87446082207643e-05, 'alpha': 0.07562657698792305}
    sen.fit(**sen_params)
    print("Done")
    """

    list_recommender = [icb, icf, ucf, p3a, rp3b]
    list_already_seen = []

    for rec_perm in combinations(list_recommender, 3):

        if rec_perm not in combinations(list_already_seen, 3):

            recommender_names = '_'.join(
                [r.RECOMMENDER_NAME for r in rec_perm])
            output_folder_path = "result_experiments_v3/seed_" + str(
                seed) + '_3--1' + '/' + recommender_names + '/'

            # If directory does not exist, create
            if not os.path.exists(output_folder_path):
                os.makedirs(output_folder_path)

            # TODO: setta I GIUSTI EVALUATOR QUI!!!!
            runParameterSearch_Collaborative_partial = partial(
                runParameterSearch_Collaborative,
                URM_train=URM_train,
                ICM_train=ICM_obj,
                metric_to_optimize="MAP",
                n_cases=50,
                n_random_starts=20,
                evaluator_validation_earlystopping=evaluator_valid_hybrid,
                evaluator_validation=evaluator_valid_hybrid,
                evaluator_test=evaluator_test,
                output_folder_path=output_folder_path,
                allow_weighting=False,
                # similarity_type_list = ["cosine", 'jaccard'],
                parallelizeKNN=False,
                list_rec=rec_perm)
            pool = multiprocessing.Pool(processes=int(
                multiprocessing.cpu_count()),
                                        maxtasksperchild=1)
            pool.map(runParameterSearch_Collaborative_partial,
                     collaborative_algorithm_list)
def read_data_split_and_search():
    """
    This function provides a simple example on how to tune parameters of a given algorithm

    The BayesianSearch object will save:
        - A .txt file with all the cases explored and the recommendation quality
        - A _best_model file which contains the trained model and can be loaded with recommender.load_model()
        - A _best_parameter file which contains a dictionary with all the fit parameters, it can be passed to recommender.fit(**_best_parameter)
        - A _best_result_validation file which contains a dictionary with the results of the best solution on the validation
        - A _best_result_test file which contains a dictionary with the results, on the test set, of the best solution chosen using the validation set
    """

    seed = 1205
    parser = DataParser()

    URM_all = parser.get_URM_all()
    ICM_obj = parser.get_ICM_all()

    # SPLIT TO GET TEST PARTITION
    URM_train, URM_test = split_train_in_two_percentage_global_sample(URM_all, train_percentage=0.90, seed=seed)

    # SPLIT TO GET THE HYBRID VALID PARTITION
    URM_train, URM_valid_hybrid = split_train_in_two_percentage_global_sample(URM_train, train_percentage=0.85,
                                                                              seed=seed)

    collaborative_algorithm_list = [
        # EASE_R_Recommender
        # PipeHybrid001,
        # Random,
        # TopPop,
        # P3alphaRecommender,
        # RP3betaRecommender,
        # ItemKNNCFRecommender,
        # UserKNNCFRecommender,
        # MatrixFactorization_BPR_Cython,
        # MatrixFactorization_FunkSVD_Cython,
        # PureSVDRecommender,
        # NMFRecommender,
        # PureSVDItemRecommender
        # SLIM_BPR_Cython,
        # SLIMElasticNetRecommender
        # IALSRecommender
        # MF_MSE_PyTorch
        # MergedHybrid000
        # LinearHybrid002ggg
        HybridCombinationSearch
    ]

    content_algorithm_list = [
        # ItemKNNCBFRecommender
    ]

    from Base.Evaluation.Evaluator import EvaluatorHoldout

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

    """
    earlystopping_keywargs = {"validation_every_n": 5,
                              "stop_on_validation": True,
                              "evaluator_object": evaluator_valid_hybrid,
                              "lower_validations_allowed": 5,
                              "validation_metric": 'MAP',
                              }

    print('IALS training...')
    ials = IALSRecommender(URM_train, verbose=False)
    ials_params = {'num_factors': 83, 'confidence_scaling': 'linear', 'alpha': 28.4278070726612,
                   'epsilon': 1.0234211788885077, 'reg': 0.0027328110246575004, 'epochs': 20}
    ials.fit(**ials_params, **earlystopping_keywargs)
    print("Done")


    print("PureSVD training...")
    psvd = PureSVDRecommender(URM_train, verbose=False)
    psvd_params = {'num_factors': 711}
    psvd.fit(**psvd_params)
    print("Done")
    """

    rp3b = RP3betaRecommender(URM_train, verbose=False)
    rp3b_params = {'topK': 1000, 'alpha': 0.38192761611274967, 'beta': 0.0, 'normalize_similarity': False}
    try:
        rp3b.load_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                        f'{rp3b.RECOMMENDER_NAME}_for_second_search')
        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(seed)}_hybrid_search/',
                        f'{rp3b.RECOMMENDER_NAME}_for_second_search')

    p3a = P3alphaRecommender(URM_train, verbose=False)
    p3a_params = {'topK': 131, 'alpha': 0.33660811631883863, 'normalize_similarity': False}
    try:
        p3a.load_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{p3a.RECOMMENDER_NAME}_for_second_search')
        print(f"{p3a.RECOMMENDER_NAME} loaded.")
    except:
        print(f"Fitting {p3a.RECOMMENDER_NAME} ...")
        p3a.fit(**p3a_params)
        print(f"done.")
        p3a.save_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{p3a.RECOMMENDER_NAME}_for_second_search')

    icf = ItemKNNCFRecommender(URM_train, verbose=False)
    icf_params = {'topK': 55, 'shrink': 1000, 'similarity': 'asymmetric', 'normalize': True, 'asymmetric_alpha': 0.0}
    try:
        icf.load_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{icf.RECOMMENDER_NAME}_for_second_search')
        print(f"{icf.RECOMMENDER_NAME} loaded.")
    except:
        print(f"Fitting {icf.RECOMMENDER_NAME} ...")
        icf.fit(**icf_params)
        print(f"done.")
        icf.save_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{icf.RECOMMENDER_NAME}_for_second_search')

    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)}_hybrid_search/',
                       f'{ucf.RECOMMENDER_NAME}_for_second_search')
        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)}_hybrid_search/',
                       f'{ucf.RECOMMENDER_NAME}_for_second_search')

    icb = ItemKNNCBFRecommender(URM_train, ICM_obj, verbose=False)
    icb_params = {'topK': 65, 'shrink': 0, 'similarity': 'dice', 'normalize': True}
    try:
        icb.load_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{icb.RECOMMENDER_NAME}_for_second_search')
        print(f"{icb.RECOMMENDER_NAME} loaded.")
    except:
        print(f"Fitting {icf.RECOMMENDER_NAME} ...")
        icb.fit(**icb_params)
        print(f"done.")
        icb.save_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{icb.RECOMMENDER_NAME}_for_second_search')

    sen = SLIMElasticNetRecommender(URM_train, verbose=False)
    sen_params = {'topK': 992, 'l1_ratio': 0.004065081925341167, 'alpha': 0.003725005053334143}
    try:
        sen.load_model(f'stored_recommenders/seed_{str(seed)}_hybrid_search/',
                       f'{sen.RECOMMENDER_NAME}_for_second_search')
        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(seed)}_hybrid_search/',
                       f'{sen.RECOMMENDER_NAME}_for_second_search')

    print("\nStart.")
    list_recommender = [icb, icf, ucf, p3a, rp3b, sen]
    list_already_seen = []
    combinations_already_seen = []
    """
    (icb, icf, p3a), (icb, icf, rp3b), (icb, icf, sen), (icb, p3a, rp3b), (icb, p3a, sen),
                                (icb, rp3b, sen), (icf, p3a, rp3b), (icf, p3a, sen)
    """

    for rec_perm in combinations(list_recommender, 3):

        if rec_perm not in combinations_already_seen:

            recommender_names = '_'.join([r.RECOMMENDER_NAME for r in rec_perm])
            output_folder_path = "result_experiments_v3/seed_" + str(
                seed) + '/linear_combination/' + recommender_names + '/'
            print(F"\nTESTING THE COMBO {recommender_names}")

            # If directory does not exist, create
            if not os.path.exists(output_folder_path):
                os.makedirs(output_folder_path)

            # TODO: setta I GIUSTI EVALUATOR QUI!!!!
            runParameterSearch_Collaborative_partial = partial(runParameterSearch_Collaborative,
                                                               URM_train=URM_train,
                                                               ICM_train=ICM_obj,
                                                               metric_to_optimize="MAP",
                                                               n_cases=50,
                                                               n_random_starts=20,
                                                               evaluator_validation_earlystopping=evaluator_valid_hybrid,
                                                               evaluator_validation=evaluator_valid_hybrid,
                                                               #evaluator_test=evaluator_test,
                                                               output_folder_path=output_folder_path,
                                                               allow_weighting=False,
                                                               # similarity_type_list = ["cosine", 'jaccard'],
                                                               parallelizeKNN=False,
                                                               list_rec=rec_perm)
            pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()), maxtasksperchild=1)
            pool.map(runParameterSearch_Collaborative_partial, collaborative_algorithm_list)
예제 #14
0
itemKNNCF = ItemKNNCFRecommender(URM_all)
itemKNNCF.fit(shrink=24, topK=10)

# recommenderCYTHON = SLIM_BPR_Cython(URM_all, recompile_cython=False)
# recommenderCYTHON.fit(epochs=2000, batch_size=100, sgd_mode='sdg', learning_rate=1e-5, topK=10)

# recommenderCB = ItemKNNCBFRecommender(URM_all, ICM_all)
# recommenderCB.fit(shrink=24, topK=10)

recommenderELASTIC = SLIMElasticNetRecommender(URM_all)
# recommenderELASTIC.fit(topK=10)
#recommenderELASTIC.save_model('model/', file_name='SLIM_ElasticNet')
recommenderELASTIC.load_model('model/', file_name='SLIM_ElasticNet')

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

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


class ItemKNNSimilarityHybridRecommender(BaseItemSimilarityMatrixRecommender):
    """ ItemKNNSimilarityHybridRecommender
예제 #15
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}),
        ]
예제 #16
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
                               }),
        ]
예제 #17
0
 def __init__(self,
              URM_train,
              ICM_train,
              submission=False,
              verbose=True,
              seed=1205):
     super(UserWiseHybrid004, self).__init__(URM_train, verbose=verbose)
     self.__recommender_segmentation = [
         ((0, 3),
          HiddenRecommender(URM_train,
                            ICM_train,
                            [(RP3betaRecommender(URM_train), {
                                'topK': 1000,
                                'alpha': 0.38192761611274967,
                                'beta': 0.0,
                                'normalize_similarity': False
                            }),
                             (ItemKNNCFRecommender(URM_train), {
                                 'topK': 100,
                                 'shrink': 1000,
                                 'similarity': 'asymmetric',
                                 'normalize': True,
                                 'asymmetric_alpha': 0.0
                             }),
                             (ItemKNNCBFRecommender(URM_train, ICM_train), {
                                 'topK': 205,
                                 'shrink': 1000,
                                 'similarity': 'cosine',
                                 'normalize': True,
                                 'feature_weighting': 'BM25'
                             })],
                            submission=submission,
                            verbose=verbose,
                            seed=seed), {
                                'alpha': 0.40426999639005445,
                                'l1_ratio': 1.0
                            }),
         ((3, 5),
          HiddenRecommender(
              URM_train,
              ICM_train, [(ItemKNNCFRecommender(URM_train), {
                  'topK': 100,
                  'shrink': 1000,
                  'similarity': 'asymmetric',
                  'normalize': True,
                  'asymmetric_alpha': 0.0
              }),
                          (UserKNNCFRecommender(URM_train), {
                              'topK': 190,
                              'shrink': 0,
                              'similarity': 'cosine',
                              'normalize': True
                          }),
                          (ItemKNNCBFRecommender(URM_train, ICM_train), {
                              'topK': 205,
                              'shrink': 1000,
                              'similarity': 'cosine',
                              'normalize': True,
                              'feature_weighting': 'BM25'
                          })],
              submission=submission,
              verbose=verbose,
              seed=seed), {
                  'alpha': 0.767469300493861,
                  'l1_ratio': 0.7325725081659069
              }),
         ((5, 10),
          HiddenRecommender(
              URM_train,
              ICM_train, [(RP3betaRecommender(URM_train), {
                  'topK': 1000,
                  'alpha': 0.38192761611274967,
                  'beta': 0.0,
                  'normalize_similarity': False
              }),
                          (ItemKNNCFRecommender(URM_train), {
                              'topK': 100,
                              'shrink': 1000,
                              'similarity': 'asymmetric',
                              'normalize': True,
                              'asymmetric_alpha': 0.0
                          }),
                          (ItemKNNCBFRecommender(URM_train, ICM_train), {
                              'topK': 205,
                              'shrink': 1000,
                              'similarity': 'cosine',
                              'normalize': True,
                              'feature_weighting': 'BM25'
                          })],
              submission=submission,
              verbose=verbose,
              seed=seed), {
                  'alpha': 0.40426999639005445,
                  'l1_ratio': 1.0
              }),
         ((10, 17),
          HiddenRecommender(
              URM_train,
              ICM_train, [(P3alphaRecommender(URM_train), {
                  'topK': 131,
                  'alpha': 0.33660811631883863,
                  'normalize_similarity': False
              }),
                          (UserKNNCFRecommender(URM_train), {
                              'topK': 190,
                              'shrink': 0,
                              'similarity': 'cosine',
                              'normalize': True
                          }),
                          (ItemKNNCBFRecommender(URM_train, ICM_train), {
                              'topK': 205,
                              'shrink': 1000,
                              'similarity': 'cosine',
                              'normalize': True,
                              'feature_weighting': 'BM25'
                          })],
              submission=submission,
              verbose=verbose,
              seed=seed), {
                  'alpha': 0.37776131907747645,
                  'l1_ratio': 0.44018901104481
              }),
         ((17, 100),
          HiddenRecommender(
              URM_train,
              ICM_train, [(ItemKNNCFRecommender(URM_train), {
                  'topK': 100,
                  'shrink': 1000,
                  'similarity': 'asymmetric',
                  'normalize': True,
                  'asymmetric_alpha': 0.0
              }),
                          (ItemKNNCBFRecommender(URM_train, ICM_train), {
                              'topK': 205,
                              'shrink': 1000,
                              'similarity': 'cosine',
                              'normalize': True,
                              'feature_weighting': 'BM25'
                          }),
                          (SLIMElasticNetRecommender(URM_train), {
                              'topK': 992,
                              'l1_ratio': 0.004065081925341167,
                              'alpha': 0.003725005053334143
                          })],
              submission=submission,
              verbose=verbose,
              seed=seed), {
                  'alpha': 0.7783657178315921,
                  'l1_ratio': 0.9570845000744118
              }),
         ((100, -1),
          HiddenRecommender(
              URM_train,
              ICM_train, [(P3alphaRecommender(URM_train), {
                  'topK': 131,
                  'alpha': 0.33660811631883863,
                  'normalize_similarity': False
              }),
                          (UserKNNCFRecommender(URM_train), {
                              'topK': 190,
                              'shrink': 0,
                              'similarity': 'cosine',
                              'normalize': True
                          }),
                          (ItemKNNCBFRecommender(URM_train, ICM_train), {
                              'topK': 205,
                              'shrink': 1000,
                              'similarity': 'cosine',
                              'normalize': True,
                              'feature_weighting': 'BM25'
                          })],
              submission=submission,
              verbose=verbose,
              seed=seed), {
                  'alpha': 0.37776131907747645,
                  'l1_ratio': 0.44018901104481
              }),
     ]
예제 #18
0
}

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)

itemCF_recommender = ItemKNNCFRecommender(URM_all)
itemCF_recommender.fit(**itemCFParam)
예제 #19
0
    def initialize_components(self):

        self.p3alphaUU = P3alphaRecommender(self.train.T)