diff --git a/implicit/nearest_neighbours.py b/implicit/nearest_neighbours.py index f3ca6bf6..01610811 100644 --- a/implicit/nearest_neighbours.py +++ b/implicit/nearest_neighbours.py @@ -23,7 +23,8 @@ def fit(self, weighted): self.similarity = all_pairs_knn(weighted, self.K, show_progress=self.show_progress).tocsr() self.scorer = NearestNeighboursScorer(self.similarity) - def recommend(self, userid, user_items, N=10, filter_items=None, recalculate_user=False): + def recommend(self, userid, user_items, + N=10, filter_already_liked_items=True, filter_items=None, recalculate_user=False): """ returns the best N recommendations for a user given its id""" # recalculate_user is ignored because this is not a model based algorithm items = N @@ -31,7 +32,8 @@ def recommend(self, userid, user_items, N=10, filter_items=None, recalculate_use items += len(filter_items) indices, data = self.scorer.recommend(userid, user_items.indptr, user_items.indices, - user_items.data, K=items) + user_items.data, K=items, + remove_own_likes=filter_already_liked_items) best = sorted(zip(indices, data), key=lambda x: -x[1]) if not filter_items: diff --git a/implicit/recommender_base.py b/implicit/recommender_base.py index 7121a454..06ec213e 100644 --- a/implicit/recommender_base.py +++ b/implicit/recommender_base.py @@ -25,7 +25,8 @@ def fit(self, item_users): pass @abstractmethod - def recommend(self, userid, user_items, N=10, filter_items=None, recalculate_user=False): + def recommend(self, userid, user_items, + N=10, filter_already_liked_items=True, filter_items=None, recalculate_user=False): """ Recommends items for a user @@ -120,11 +121,15 @@ def __init__(self): # cache of item norms (useful for calculating similar items) self._item_norms = None - def recommend(self, userid, user_items, N=10, filter_items=None, recalculate_user=False): + def recommend(self, userid, user_items, + N=10, filter_already_liked_items=True, filter_items=None, recalculate_user=False): user = self._user_factor(userid, user_items, recalculate_user) # calculate the top N items, removing the users own liked items from the results - liked = set(user_items[userid].indices) + if filter_already_liked_items is True: + liked = set(user_items[userid].indices) + else: + liked = set() scores = self.item_factors.dot(user) if filter_items: liked.update(filter_items)