Skip to content

Commit

Permalink
Added recommend_batch()
Browse files Browse the repository at this point in the history
  • Loading branch information
myui committed Dec 10, 2024
1 parent 37fb69c commit 39ed1fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
32 changes: 32 additions & 0 deletions rtrec/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,38 @@ def _recommend(self, user_id: int, candidate_item_ids: List[int], top_k: int = 1
"""
raise NotImplementedError("_recommend method must be implemented in the derived class")

def recommend_batch(self, users: List[Any], top_k: int = 10, filter_interacted: bool = True) -> List[List[Any]]:
"""
Recommend top-K items for a list of users.
:param users: List of user indices
:param top_k: Number of top items to recommend
:param filter_interacted: Whether to filter out items the user has already interacted with
:return: List of top-K item indices recommended for each user
"""
user_ids = [self.user_ids.get_id(user) for user in users]
interaction_matrix = self.interactions.to_csr(select_users=user_ids)

results = []
for user_id in user_ids:
if user_id is None:
results.append([]) # TODO: return popoular items?
continue
recommended_item_ids = self._recommend_batch(user_id, interaction_matrix, top_k=top_k, filter_interacted=filter_interacted)
results.append([self.item_ids.get(item_id) for item_id in recommended_item_ids])
return results

def _recommend_batch(self, user_id: int, interaction_matrix: csc_matrix, candidate_item_ids: Optional[List[int]]=None, top_k: int = 10, filter_interacted: bool = True) -> List[int]:
"""
Recommend top-K items for a list of users.
:param user_id: User index
:param interaction_matrix: Sparse user-item interaction matrix
:param candidate_item_ids: List of candidate item indices
:param top_k: Number of top items to recommend
:param filter_interacted: Whether to filter out items the user has already interacted with
:return: List of top-K item indices recommended for each user
"""
raise NotImplementedError("_recommend_batch method must be implemented in the derived class")

def similar_items(self, query_item: Any, top_k: int = 10) -> List[Any]:
"""
Find similar items for a list of query items.
Expand Down
4 changes: 3 additions & 1 deletion rtrec/models/slim.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def fit(self, user_interactions: Iterable[Tuple[Any, Any, float, float]], update
interaction_matrix = self.interactions.to_csc(item_ids)
self.model.partial_fit_items(interaction_matrix, item_ids)

@override
def _bulk_fit(self, interaction_matrix: csc_matrix) -> None:
"""
Fit the recommender model on the given interaction matrix.
Expand All @@ -46,6 +45,9 @@ def _recommend(self, user_id: int, candidate_item_ids: List[int], top_k: int = 1
interaction_matrix = self.interactions.to_csr(select_users=[user_id])
return self.model.recommend(user_id, interaction_matrix, candidate_item_ids, top_k=top_k, filter_interacted=filter_interacted)

def _recommend_batch(self, user_id, interaction_matrix, candidate_item_ids = None, top_k = 10, filter_interacted = True):
return self.model.recommend(user_id, interaction_matrix, candidate_item_ids, top_k=top_k, filter_interacted=filter_interacted)

def _similar_items(self, query_item_id: int, top_k: int = 10) -> List[int]:
"""
Find similar items for a list of query items.
Expand Down
2 changes: 1 addition & 1 deletion rtrec/recommender.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def recommend_batch(self, users: List[Any], top_k: int = 10, filter_interacted:
:param filter_interacted: Whether to filter out items the user has already interacted with
:return: List of top-K item indices recommended for each user
"""
return [self.recommend(user, top_k, filter_interacted) for user in users]
return self.model.recommend_batch(users, top_k, filter_interacted)

def similar_items(self, query_items: List[Any], top_k: int = 10) -> List[List[Any]]:
"""
Expand Down

0 comments on commit 39ed1fc

Please sign in to comment.