From fd984bc251e468e092ddf2bc7395127c21336482 Mon Sep 17 00:00:00 2001 From: ita Date: Mon, 16 Jul 2018 02:14:00 +0900 Subject: [PATCH 1/2] add an option to whether to include previously liked items or not --- implicit/nearest_neighbours.py | 6 ++++-- implicit/recommender_base.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) 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..2bac604b 100644 --- a/implicit/recommender_base.py +++ b/implicit/recommender_base.py @@ -1,4 +1,4 @@ -""" Base class for recommendation algorithms in this package """ +t1""" Base class for recommendation algorithms in this package """ import itertools from abc import ABCMeta, abstractmethod @@ -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) From c61e2f7556aa0b2e8b0aee421612fabef01da64a Mon Sep 17 00:00:00 2001 From: ita Date: Mon, 16 Jul 2018 02:26:34 +0900 Subject: [PATCH 2/2] Fix typo --- implicit/recommender_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implicit/recommender_base.py b/implicit/recommender_base.py index 2bac604b..06ec213e 100644 --- a/implicit/recommender_base.py +++ b/implicit/recommender_base.py @@ -1,4 +1,4 @@ -t1""" Base class for recommendation algorithms in this package """ +""" Base class for recommendation algorithms in this package """ import itertools from abc import ABCMeta, abstractmethod