def objective(params):
    print("\n############## New iteration ##############\n", params)
    if kfold:
        loss = - RunRecommender.evaluate_hybrid_weights_validation_kfold(recommender_list, params, kfold=N_KFOLD, parallelize_evaluation=kfold, parallel_fit=False)
    else:
        loss = - RunRecommender.evaluate_hybrid_weights_validation(hybrid, params)
    return loss
def objective(params):
    print('###########################################')
    params["topK"] = int(params["topK"])
    print("Current parameters:")
    print(params)
    loss = - RunRecommender.evaluate_on_validation_set(ItemCollaborativeFilter, params, Kfold=N_K_FOLD, parallel_fit=False, parallelize_evaluation=True)
    return loss
def objective(params):
    params["topK"] = int(params["topK"])
    print("############ Current parameters ############")
    print(params)
    loss = -RunRecommender.evaluate_on_validation_set(
        RP3betaRecommender,
        params,
        Kfold=N_KFOLD,
        parallelize_evaluation=True,
        user_group="warm")
    return loss
Example #4
0
def objective(params):
    print('###########################################')
    params["n_factors"] = int(params["n_factors"])
    print("Current parameters:")
    print(params)
    loss = -RunRecommender.evaluate_on_validation_set(
        AlternatingLeastSquare,
        params,
        Kfold=N_K_FOLD,
        parallel_fit=False,
        parallelize_evaluation=True,
        user_group="warm")
    return loss
def objective(params):
    params["topK"] = int(params["topK"])
    params["batch_size"] = int(params["batch_size"])
    params["random_seed"] = 1234
    params["epochs"] = 30
    print("############ Current parameters ############")
    print(params)
    loss = -RunRecommender.evaluate_on_validation_set(
        SLIM_BPR_Cython,
        params,
        Kfold=N_KFOLD,
        parallelize_evaluation=False,
        user_group="warm")
    return loss
Example #6
0
def objective(params):
    print('###########################################')
    print(params)
    params["topK"] = int(params["topK"])
    # if type(params["similarity"]) == tuple:
    #     params["asymmetric_alpha"] = params["similarity"][1]
    #     params["similarity"] = params["similarity"][0]
    loss = -RunRecommender.evaluate_on_validation_set(
        HybridUserCBFRegionalTopPop,
        params,
        Kfold=10,
        user_group="cold",
        parallel_fit=False,
        parallelize_evaluation=True)
    return loss
def objective(params):
    print('###########################################')
    print(params)
    loss = - RunRecommender.evaluate_on_validation_set(UserCBF, params, Kfold=4, user_group="cold")
    return loss
# last_opt = {'SSLIM_weight': 0.8737840927419455, 'item_cbf_weight': 0.037666643326618406, 'item_cf_weight': 0.014294955186782246, 'rp3_weight': 0.9314974601074552, 'user_cbf_weight': 0, 'user_cf_weight': 0}
#
opt = {'AlternatingLeastSquare': 0.07611985905191196, 'ItemCBF': 0.017561491230314447, 'ItemCollaborativeFilter': 0.0341817493248531, 'RP3betaRecommender': 0.9713719890744753, 'SLIMElasticNetRecommender': 0.9974897962716185, 'SLIM_BPR_Recommender': 0.8633266021278376}

# Optimize
best = fmin(fn=objective, space=search_space, algo=hp.tpe.suggest,
            max_evals=MAX_EVALS, trials=bayes_trials, verbose=True, points_to_evaluate=[opt])

best = space_eval(search_space, best)

# best will the return the the best hyperparameter set

print("\n############## Best Parameters ##############\n")
print(best, "\n\nEvaluating on test set now...")

RunRecommender.evaluate_on_test_set(Hybrid, {"weights": best}, Kfold=N_KFOLD,
                                    init_params={"recommenders": [MultiThreadSLIM_ElasticNet, RP3betaRecommender, ItemCBF, AlternatingLeastSquare, SLIM_BPR_Cython]},
                                    parallelize_evaluation=False,
                                    parallel_fit=False)

computer_sleep(verbose=False)


####################################################
# Test Map to beat 0.05112213282549001             #
# MAP-10 score: 0.05107393648464094 on kfold, k = 4#
####################################################

new_best = {'ItemCBF': 0.013769403495491125, 'ItemCollaborativeFilter': 0.015447034894805844, 'RP3betaRecommender': 0.9945281573130214, 'SLIMElasticNetRecommender': 0.7629862396511091}

# {'SLIM_weight': 0.7989266787188458, 'item_cbf_weight': 0.03258554983815878, 'item_cf_weight': 0.0077609799300920445, 'rp3_weight': 0.6740989817682256}
    loss = - RunRecommender.evaluate_on_validation_set(UserCBF, params, Kfold=4, user_group="cold")
    return loss

user_cbf_space = {
    "topK": hp.hp.choice('topK', np.arange(0, 500, 5)),
    "shrink": hp.hp.uniformint('shrink', 0, 50),
    "similarity": hp.hp.choice('similarity', ["cosine", "adjusted", "asymmetric", "pearson", "jaccard", "dice", "tversky", "tanimoto"]),
    "suppress_interactions": hp.hp.choice('suppress_interactions', [True, False]),
    "normalize": hp.hp.choice('normalize', [True, False])
}

if __name__ == '__main__':
    ### step 3 : storing the results of every iteration
    bayes_trials = Trials()
    MAX_EVALS = 100

    previous = {'normalize': True, 'shrink': 5, 'similarity': "cosine", 'suppress_interactions': False, 'topK': 200}
    last = {'normalize': True, 'shrink': 1.0, 'similarity': "dice", 'suppress_interactions': True, 'topK': 93*5}

    # Optimize
    best = fmin(fn=objective, space=user_cbf_space, algo=hp.tpe.suggest,
                max_evals=MAX_EVALS, trials=bayes_trials, verbose=True, points_to_evaluate=[previous, last])

    ### best will the return the the best hyperparameter set

    print(best)



    MAP = RunRecommender.evaluate_on_test_set(UserCBF, best, Kfold=4, user_group="cold")
            rows.extend(rows_)
            cols.extend(cols_)

        # generate the sparse weight matrix
        print("Now generating W matrix...")
        self.W_sparse = sps.csr_matrix((values, (rows, cols)),
                                       shape=(n_items, n_items),
                                       dtype=np.float32)
        print("Done!")


if __name__ == '__main__':

    from utils.helper import Helper
    helper = Helper()

    slim = MultiThreadSSLIM_ElasticNet

    best = {
        'alpha': 0.0024081648139725204,
        'l1_ratio': 0.0007553368138338653,
        'positive_only': False,
        'side_alpha': 3.86358712510434,
        'topK': 65,
        'random_state': 1234
    }

    map_10 = RunRecommender.evaluate_on_validation_set(slim, best)

    print(map_10)
        :param user_id: array of user ids for which to compute recommendations
        :param scores: array containing the scores for each object"""

        start_pos = self.URM_train.indptr[user_id]
        end_pos = self.URM_train.indptr[user_id + 1]

        user_profile = self.URM_train.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":
    # Train and test data are now loaded by the helper

    #weights = {'SLIM_weight': 0.8950096358670148, 'item_cbf_weight': 0.034234727663263104, 'item_cf_weight': 0.011497379340447589, 'rp3_weight': 0.8894480634395567}

    #weights2 = {'SLIM_weight': 0.8525330515257261, 'item_cbf_weight': 0.03013686377319209, 'item_cf_weight': 0.01129668459365759, 'rp3_weight': 0.9360587800999112}
    weights = {'SLIM_weight': 0.8737840927419455, 'item_cbf_weight': 0.037666643326618406, 'item_cf_weight': 0.014294955186782246, 'rp3_weight': 0.9314974601074552}

    hybrid_ucficf = HybridElasticNetICFUCFRP3Beta

    # Evaluation is performed by RunRecommender
    # RunRecommender.evaluate_on_test_set(hybrid_ucficf, weights)

    RunRecommender.evaluate_on_test_set(hybrid_ucficf, weights, Kfold=4, parallelize_evaluation=True)
    #RunRecommender.evaluate_on_validation_set(hybrid_ucficf, weights)

    RunRecommender.run(hybrid_ucficf, weights)
Example #12
0
        print("########################## Group", group_id,
              "##########################")

        groups.append("Group {}, average p.len {:.2f}, min {}, max {}".format(
            group_id, users_in_group_p_len.mean(), users_in_group_p_len.min(),
            users_in_group_p_len.max()))

        users_not_in_group_flag = np.isin(sorted_users,
                                          users_in_group,
                                          invert=True)
        users_not_in_group = sorted_users[users_not_in_group_flag]

        users_in_group = set(list(users_in_group.astype(int)))
        print("SLIM ElasticNet")
        MAP_slim_per_group.append(
            RunRecommender.evaluate_profile(slim,
                                            users_to_evaluate=users_in_group))
        print("Item Based CF")
        MAP_item_cf_per_group.append(
            RunRecommender.evaluate_profile(item_based,
                                            users_to_evaluate=users_in_group))
        print("User Based CF")
        MAP_user_cf_per_group.append(
            RunRecommender.evaluate_profile(user_based,
                                            users_to_evaluate=users_in_group))
        print("User Based CBF")
        MAP_user_based_CBF_per_group.append(
            RunRecommender.evaluate_profile(user_cbf,
                                            users_to_evaluate=users_in_group))
        print("Item Based CBF")
        MAP_item_based_CBF_per_group.append(
            RunRecommender.evaluate_profile(item_cbf,
            scores = self.filter_seen(user_id, scores)
        recommended_items = np.argsort(scores)
        recommended_items = np.flip(recommended_items, axis=0)
        return recommended_items[:at]

    def filter_seen(self, user_id, scores):
        start_pos = self.URM_train.indptr[user_id]
        end_pos = self.URM_train.indptr[user_id + 1]

        user_profile = self.URM_train.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":

    cbf_recommender = ItemCBF

    params = {
        'normalize': True,
        'shrink': 18,
        'similarity': 'asymmetric',
        'topK': 15
    }

    RunRecommender.evaluate_on_validation_set(cbf_recommender, params)
    RunRecommender.evaluate_on_validation_set(cbf_recommender, params)
    # RunRecommender.run(cbf_recommender)
Example #14
0
                space=search_space,
                algo=hp.tpe.suggest,
                max_evals=MAX_EVALS,
                trials=bayes_trials,
                verbose=True,
                points_to_evaluate=opt_eval)

    params = space_eval(search_space, best)

    ### best will the return the the best hyperparameter set

    print("Best parameters:")
    print(params)
    params["topK"] = int(params["topK"])

    ### best will the return the the best hyperparameter set

    params = space_eval(search_space, best)
    print("############### Best parameters ###############")
    print(params)

    print("############### Test set performance ###############")
    RunRecommender.evaluate_on_test_set(AlternatingLeastSquare,
                                        params,
                                        parallel_fit=False,
                                        Kfold=N_K_FOLD,
                                        parallelize_evaluation=True,
                                        user_group="warm")

    computer_sleep(verbose=False)
    return loss

item_cbf_space = {
    "topK": hp.hp.choice('topK', np.arange(0, 500, 5)),
    "shrink": hp.hp.uniformint('shrink', 0, 50),
    "similarity": hp.hp.choice('similarity', ["cosine", "jaccard", "dice"]),
    "bm_25_normalization": hp.hp.choice('bm_25_normalization', [True, False]),

    "normalize": hp.hp.choice('normalize', [True, False])
}


if __name__ == '__main__':
    ### step 3 : storing the results of every iteration
    bayes_trials = Trials()
    MAX_EVALS = 100

    # Optimize
    best = fmin(fn=objective, space=item_cbf_space, algo=hp.tpe.suggest,
                max_evals=MAX_EVALS, trials=bayes_trials, verbose=True)

    ### best will the return the the best hyperparameter set

    params = space_eval(item_cbf_space, best)

    print('################### Best parameters ######################')
    print("Parameters:", params)

    print('################### Performance on Test set ######################')
    RunRecommender.evaluate_on_test_set(UserCBF, params, Kfold=N_FOLD, parallelize_evaluation=True, user_group="warm")
Example #16
0
}

if __name__ == '__main__':
    ### step 3 : storing the results of every iteration
    bayes_trials = Trials()
    MAX_EVALS = 100

    # Optimize
    best = fmin(fn=objective,
                space=item_cbf_space,
                algo=hp.tpe.suggest,
                max_evals=MAX_EVALS,
                trials=bayes_trials,
                verbose=True,
                points_to_evaluate={
                    "topK": 1,
                    "shrink": 13,
                    "similarity": "cosine",
                    "normalize": True
                })

    ### best will the return the the best hyperparameter set

    params = space_eval(item_cbf_space, best)

    print("Best parameters:", params)
    RunRecommender.evaluate_on_test_set(ItemCBF,
                                        params,
                                        Kfold=4,
                                        parallel_fit=True)
Example #17
0
def objective(params):
    print('###########################################')
    print(params)
    loss = -RunRecommender.evaluate_on_validation_set(
        ItemCBF, params, Kfold=4, parallel_fit=True)
    return loss
        return scores

    def recommend(self, playlist_id, at=10, exclude_seen=True):
        # Compute scores of the recommendation
        scores = self.compute_scores(playlist_id)

        # Filter to exclude already seen items
        if exclude_seen:
            scores = self.filter_seen(playlist_id, scores)
        recommended_items = np.argsort(scores)
        recommended_items = np.flip(recommended_items, axis=0)
        return recommended_items[:at]

    def filter_seen(self, playlist_id, scores):
        start_pos = self.URM.indptr[playlist_id]
        end_pos = self.URM.indptr[playlist_id + 1]

        user_profile = self.URM.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":
    cbf_recommender = CBFRecomender()
    evaluator = Evaluator()
    evaluator.split_data_randomly()
    runner = RunRecommender()
    runner.run(cbf_recommender)
Example #19
0
        if exclude_seen:
            unseen_items_mask = np.in1d(self.popular_items,
                                        self.URM_CSR[user_id].indices,
                                        assume_unique=True,
                                        invert=True)

            unseen_items = self.popular_items[unseen_items_mask]

            recommended_items = unseen_items[0:at]

        else:
            recommended_items = self.popular_items[0:at]

        return recommended_items


if __name__ == "__main__":

    # evaluator = Evaluator()
    # evaluator.split_data_randomly_2()

    top_popular = TopPopRecommender

    map10 = RunRecommender.evaluate_on_test_set(top_popular, {},
                                                Kfold=4,
                                                parallel_fit=True,
                                                user_group="cold")

    #print('{0:.128f}'.format(map10))
    print(map10)
Example #20
0
    item_popularity = (self.URM_CSR > 0).sum(axis=0)
    # Squeeze term removes single-dimensional entries from the shape of an array.
    item_popularity = np.array(item_popularity).squeeze()

    # Ordering the items according to the popularity values
    self.popular_items = np.argsort(item_popularity)

    # Flip order high to lower
    self.popular_items = np.flip(self.popular_items, axis=0)

  def recommend(self, playlist_id, at=10, remove_seen = True):
    if remove_seen:
      unseen_items_mask = np.in1d(self.popular_items, self.URM_CSR[playlist_id].indices,  assume_unique=True, invert=True)

      unseen_items = self.popular_items[unseen_items_mask]

      recommended_items = unseen_items[0:at]

    else:
      recommended_items = self.popularItems[0:at]

    return recommended_items



if __name__ == "__main__":
  helper = Helper()
  top_popular = TopPopRecommender(helper)
  runner = RunRecommender()
  runner.run(top_popular)
        if exclude_seen:
            scores = self.filter_seen(user_id, scores)

        # rank items
        ranking = scores.argsort()[::-1]

        return ranking[:at]

    def filter_seen(self, user_id, scores):

        start_pos = self.URM_train.indptr[user_id]
        end_pos = self.URM_train.indptr[user_id + 1]

        user_profile = self.URM_train.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":

    # evaluator = Evaluator()
    # evaluator.split_data_randomly()

    user_cf_parameters = {"topK": 410, "shrink": 0}

    cb = UserCollaborativeFilter

    map10 = RunRecommender.run(cb, user_cf_parameters)
Example #22
0
    def recommend(self, user_id, at=10, exclude_seen=True):

        scores = self.compute_scores(user_id)

        if exclude_seen:
            scores = self.filter_seen(user_id, scores)

        # rank items
        ranking = scores.argsort()[::-1]

        return ranking[:at]

    def filter_seen(self, user_id, scores):
        start_pos = self.URM_train.indptr[user_id]
        end_pos = self.URM_train.indptr[user_id + 1]

        user_profile = self.URM_train.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":
    from utils.run import RunRecommender

    cb = ItemCollaborativeFilter

    map10 = RunRecommender.evaluate_on_test_set(cb, {})
Example #23
0
from SLIM_BPR.Cython.SLIM_BPR_Cython import SLIM_BPR_Cython
from utils.run import RunRecommender

recommender = SLIM_BPR_Cython

RunRecommender.evaluate_on_test_set(SLIM_BPR_Cython, {})
Example #24
0
def objective(params):
    print("Current parameters:")
    print(params)
    loss = - RunRecommender.evaluate_hybrid_weights_validation(hybrid, params)
    return loss
if __name__ == '__main__':
    ### step 3 : storing the results of every iteration
    bayes_trials = Trials()

    # Optimize
    best = fmin(fn=objective,
                space=slim_bpr_space,
                algo=hp.tpe.suggest,
                max_evals=MAX_EVALS,
                trials=bayes_trials,
                verbose=True)

    ### best will the return the the best hyperparameter set

    params = space_eval(slim_bpr_space, best)

    params["topK"] = int(params["topK"])
    params["batch_size"] = int(params["batch_size"])
    params["random_seed"] = 1234
    params["epochs"] = 30

    print("############### Best parameters ###############")

    print(params)
    RunRecommender.evaluate_on_test_set(SLIM_BPR_Cython,
                                        params,
                                        Kfold=N_KFOLD,
                                        parallelize_evaluation=False,
                                        user_group="warm")
Example #26
0
        # Compute scores of the recommendation
        scores = self.compute_scores(user_id)

        # Filter to exclude already seen items
        if exclude_seen:
            scores = self.filter_seen(user_id, scores)
        recommended_items = np.argsort(scores)
        recommended_items = np.flip(recommended_items, axis=0)
        return recommended_items[:at]

    def filter_seen(self, user_id, scores):
        start_pos = self.URM_train.indptr[user_id]
        end_pos = self.URM_train.indptr[user_id + 1]

        user_profile = self.URM_train.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":

    # evaluator.split_data_randomly()

    parameters = {"topK": 4, "shrink": 8}
    cbf_recommender = AssetCBF

    RunRecommender.evaluate_on_test_set(cbf_recommender, parameters)
    # RunRecommender.run(cbf_recommender)
Example #27
0
        return recommended_items[0: at]

    def filter_seen(self, user_id, scores):
        """Remove items that are in the user profile from recommendations

        :param user_id: array of user ids for which to compute recommendations
        :param scores: array containing the scores for each object"""

        start_pos = self.URM_train.indptr[user_id]
        end_pos = self.URM_train.indptr[user_id + 1]

        user_profile = self.URM_train.indices[start_pos:end_pos]

        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":
    # Train and test data are now loaded by the helper

    weights = {'SSLIM_weight': 0.35630766332989877, 'item_cbf_weight': 0.0016397036630690053, 'item_cf_weight': 0.028077081167200757, 'rp3_weight': 0.6615055983108671}

    hybrid_ucficf = HybridSSLIMICFUCFRP3Beta

    RunRecommender.run(hybrid_ucficf, weights)

    #RunRecommender.evaluate_on_test_set(hybrid_ucficf, weights, Kfold=10, parallelize_evaluation=True)


        'topK': 56
    }

    # RunRecommender.evaluate_on_validation_set(RP3betaRecommender, opt)
    # Optimize
    best = fmin(fn=objective,
                space=als_space,
                algo=hp.tpe.suggest,
                max_evals=MAX_EVALS,
                trials=bayes_trials,
                verbose=True,
                points_to_evaluate=[opt])

    ### best will the return the the best hyperparameter set

    best = space_eval(als_space, best)

    print("############### Best parameters ###############")

    print(best)
    RunRecommender.evaluate_on_test_set(RP3betaRecommender,
                                        best,
                                        Kfold=N_KFOLD,
                                        parallelize_evaluation=True,
                                        user_group="warm")

    computer_sleep(verbose=False)
    """ 
    MAP to beat: .03581871259565834 val
    .041 test
    """
def objective(params):
    print('###########################################')
    print(params)
    loss = - RunRecommender.evaluate_on_validation_set(UserCBF, params, Kfold=N_FOLD, parallel_fit=False, parallelize_evaluation=True, user_group="warm")
    return loss
Example #30
0
        scores[user_profile] = -np.inf

        return scores


if __name__ == "__main__":
    # Train and test data are now loaded by the helper
    # weights = {"weights": {'AlternatingLeastSquare': 0.09136760425375567, 'ItemCBF': 0.01686781824511765, 'ItemCollaborativeFilter': 0.03454041362675262, 'RP3betaRecommender': 0.8187162817070645, 'SLIMElasticNetRecommender': 0.7756431422518303}}
    weights = {
        "weights": {
            'ItemCBF': 0.013769403495491125,
            'ItemCollaborativeFilter': 0.015447034894805844,
            'RP3betaRecommender': 0.9945281573130214,
            'SLIMElasticNetRecommender': 0.7629862396511091
        }
    }
    # weights = {"weights": {'RP3betaRecommender': 1}}

    RunRecommender.run(Hybrid,
                       weights,
                       init_params={
                           "recommenders": [
                               MultiThreadSLIM_ElasticNet,
                               ItemCollaborativeFilter, RP3betaRecommender,
                               ItemCBF
                           ]
                       })
    #RunRecommender.run(Hybrid, weights, init_params={"recommenders": [RP3betaRecommender]})

    #RunRecommender.evaluate_on_test_set(hybrid_ucficf, weights, Kfold=10, parallelize_evaluation=True)