From 0e7a88ecdc3effa8608698314d89966b603cda96 Mon Sep 17 00:00:00 2001 From: Victor Coscrato <30760523+vcoscrato@users.noreply.github.com> Date: Wed, 4 Dec 2019 20:15:17 +0000 Subject: [PATCH] Add ranking function to user on algo_base Add a method to generate a recommendation ranking for a user. In addition, allows to choose among a specific set of items and to remove items already rated by the user. --- surprise/prediction_algorithms/algo_base.py | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/surprise/prediction_algorithms/algo_base.py b/surprise/prediction_algorithms/algo_base.py index 4bb82fa5..42ce95f8 100644 --- a/surprise/prediction_algorithms/algo_base.py +++ b/surprise/prediction_algorithms/algo_base.py @@ -285,3 +285,37 @@ def get_neighbors(self, iid, k): k_nearest_neighbors = [j for (j, _) in others[:k]] return k_nearest_neighbors + + def rank(self, uid, n=10, iid_list=None, remove_rated=True): + """Build a recommendation rank for a given user. + + Args: + uid: (Raw) id of the user. See :ref:`this note`. + n(int): The desired number of recommendations. + iid_list(list): A list containing the (Raw) iids to be considered when + ranking. If None, consider all items. + remove_rated(bool): If True items already rated by the user wont be + part of the ranking. + + Returns: + A list containing tuples: the (Raw) ids of the predicted item, its + estimated rating and uncertainty std. The list is in decreasing + order of preference. + """ + if iid_list: + item_set = [self.trainset.to_inner_iid(i) for i in iid_list] + else: + item_set = self.trainset.all_items() + + if remove_rated: + item_set = [i for i in item_set if i not in + [i[0] for i in self.trainset.ur[0]]] + + iuid = self.trainset.to_inner_uid(uid) + preds = [[iiid, self.estimate(iuid, iiid)] for iiid in item_set] + est = [i[1] for i in preds] + sort_idx = sorted(range(len(est)), key=est.__getitem__, reverse=True) + rank = [[self.trainset.to_raw_iid(preds[i][0]), preds[i][1]] for i in + sort_idx[:n]] + + return rank