Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to whether to include previously liked items or not #140

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions implicit/nearest_neighbours.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ 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
if filter_items:
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:
Expand Down
11 changes: 8 additions & 3 deletions implicit/recommender_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down