def __init__(self, urm_train, eurm=False):
        super(HybridGenRecommender, 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_2 = UserKNNCBFRecommender(urm_train, ucm_all)
        # recommender_2.fit(shrink=500, topK=1600, normalize=True)

        recommender_2 = UserKNNCBFRecommender(urm_train, ucm_all)
        recommender_2.fit(shrink=1777,
                          topK=1998,
                          similarity='tversky',
                          feature_weighting='BM25',
                          tversky_alpha=0.1604953616,
                          tversky_beta=0.9862348646)

        self.recommender_1 = recommender_1
        self.recommender_2 = recommender_2
示例#2
0
    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)
from DataManager.split_train_validation_leave_k_out import split_train_leave_k_out_user_wise
from FeatureWeighting.CFW_D_Similarity_Linalg import CFW_D_Similarity_Linalg
from FeatureWeighting.User_CFW_D_Similarity_Linalg import User_CFW_D_Similarity_Linalg
from Utils.s_plus import dot_product
import scipy.sparse as sps
from Base.Evaluation.Evaluator import EvaluatorHoldout
from GraphBased.RP3betaRecommender import RP3betaRecommender
from ParameterTuning.SearchBayesianSkopt import SearchBayesianSkopt
from skopt.space import Real, Integer, Categorical
from ParameterTuning.SearchAbstractClass import SearchInputRecommenderArgs

Data = DataManager()

ucm_age, ucm_region, ucm_all = Data.get_ucm()

icm_price, icm_asset, icm_sub, icm_all = Data.get_icm()

urm_train, urm_test = split_train_leave_k_out_user_wise(Data.get_urm(),
                                                        temperature='normal')
urm_train, urm_valid = split_train_leave_k_out_user_wise(urm_train,
                                                         temperature='valid')
evaluator_test = EvaluatorHoldout(urm_test, cutoff_list=[10])
evaluator_valid = EvaluatorHoldout(urm_valid, cutoff_list=[10])

recommender_4 = RP3betaRecommender(urm_train)
recommender_4.fit(topK=16,
                  alpha=0.03374950051351756,
                  beta=0.24087176329409027,
                  normalize_similarity=True)

# recommender_3 = UserKNNCFRecommender(urm_train)
    def __init__(self, urm_train, eurm=False):
        super(HybridGen2Recommender, self).__init__(urm_train)
        self.eurm = eurm
        self.data_folder = Path(__file__).parent.parent.absolute()

        self.num_users = urm_train.shape[0]

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

        data = DataManager()

        if Path(self.data_folder /
                'Data/icm_sparse.npz').is_file() and self.eurm:
            print("Previous icm_sparse found")
        else:
            _, _, _, icm_all = data.get_icm()

            args = {
                'topK': 6,
                'shrink': 5,
                'feature_weighting': 'TF-IDF',
                'similarity': 'cosine',
                'normalize': False
            }

            recommender_1 = ItemKNNCBFRecommender(urm_train, icm_all)
            recommender_1.fit(**args)
            self.recommender_1 = recommender_1

        if Path(self.data_folder /
                'Data/ucm_sparse.npz').is_file() and self.eurm:
            print("Previous ucm_sparse found")
        else:
            ucm_age, ucm_region, ucm_all = data.get_ucm()
            recommender_2 = UserKNNCBFRecommender(urm_train, ucm_all)
            recommender_2.fit(shrink=1777,
                              topK=1998,
                              similarity='tversky',
                              feature_weighting='BM25',
                              tversky_alpha=0.1604953616,
                              tversky_beta=0.9862348646)
            self.recommender_2 = recommender_2

        if self.eurm:
            beta = 0.6
            if Path(self.data_folder / 'Data/icm_sparse.npz').is_file():
                self.score_matrix_1 = sps.load_npz(self.data_folder /
                                                   'Data/icm_sparse.npz')
            else:
                print("icm_sparse not found, create new one...")
                self.score_matrix_1 = self.recommender_1._compute_item_matrix_score(
                    np.arange(self.num_users))
                user_score_matrix_1 = normalize(self.score_matrix_1,
                                                norm='max',
                                                axis=1)
                item_score_matrix_1 = normalize(self.score_matrix_1.tocsc(),
                                                norm='max',
                                                axis=0)
                self.score_matrix_1 = item_score_matrix_1 * beta + user_score_matrix_1.tocsr(
                ) * (1 - beta)
                sps.save_npz(self.data_folder / 'Data/icm_sparse.npz',
                             self.score_matrix_1)

            if Path(self.data_folder / 'Data/ucm_sparse.npz').is_file():
                self.score_matrix_2 = sps.load_npz(self.data_folder /
                                                   'Data/ucm_sparse.npz')
            else:
                print("ucm_sparse not found, create new one...")
                self.score_matrix_2 = self.recommender_2._compute_item_matrix_score(
                    np.arange(self.num_users))
                user_score_matrix_2 = normalize(self.score_matrix_2,
                                                norm='max',
                                                axis=1)
                item_score_matrix_2 = normalize(self.score_matrix_2.tocsc(),
                                                norm='max',
                                                axis=0)
                self.score_matrix_2 = item_score_matrix_2 * beta + user_score_matrix_2.tocsr(
                ) * (1 - beta)
                sps.save_npz(self.data_folder / 'Data/ucm_sparse.npz',
                             self.score_matrix_2)