Skip to content

Commit 9e6387a

Browse files
authored
Fix rank() function to work with arbitrary item_indices (PreferredAI#363)
1 parent 1c834ff commit 9e6387a

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

cornac/models/recommender.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,12 @@ def rank(self, user_idx, item_indices=None):
246246
item_indices: 1d array, optional, default: None
247247
A list of candidate item indices to be ranked by the user.
248248
If `None`, list of ranked known item indices and their scores will be returned.
249-
ASSUMPTION: list of item indices are continuous from 0 to len(item_indices).
250249
251250
Returns
252251
-------
253-
Tuple of `item_rank`, and `item_scores`. The order of values
254-
in item_scores are corresponding to the order of their ids in item_ids
255-
252+
(item_rank, item_scores): tuple
253+
`item_rank` contains item indices being ranked by their scores.
254+
`item_scores` contains scores of items corresponding to their indices in the `item_indices` input.
256255
"""
257256
# obtain item scores from the model
258257
try:
@@ -277,9 +276,8 @@ def rank(self, user_idx, item_indices=None):
277276
item_scores = all_item_scores[: self.train_set.num_items]
278277
item_rank = item_scores.argsort()[::-1]
279278
else:
280-
item_scores = all_item_scores[: len(item_indices)]
281-
item_rank = item_scores.argsort()[::-1]
282-
item_scores = item_scores[item_indices]
279+
item_scores = all_item_scores[item_indices]
280+
item_rank = np.array(item_indices)[item_scores.argsort()[::-1]]
283281

284282
return item_rank, item_scores
285283

0 commit comments

Comments
 (0)