Truncate recommend() result to caller's N when filter_items is set (#736)#757
Open
Cyberfilo wants to merge 1 commit into
Open
Truncate recommend() result to caller's N when filter_items is set (#736)#757Cyberfilo wants to merge 1 commit into
Cyberfilo wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
`ItemItemRecommender.recommend(N=K, filter_items=F)` was returning up to `K + len(F)` items instead of `K`. Fixes #736.
Root cause
In `implicit/nearest_neighbours.py::ItemItemRecommender.recommend`, the post-mask slice reused an inflated `N` that was meant only for the over-fetch step:
```python
if filter_items is not None:
N += len(filter_items)
...
if filter_items is not None:
mask = np.isin(ids, filter_items, invert=True)
ids, scores = ids[mask][:N], scores[mask][:N] # uses inflated N
```
Fix
Store the caller-requested `N` before inflating, then truncate to that value after masking:
```python
requested_n = N
if filter_items is not None:
N += len(filter_items)
...
if filter_items is not None:
mask = np.isin(ids, filter_items, invert=True)
ids, scores = ids[mask][:requested_n], scores[mask][:requested_n]
```
The over-fetch is still required to ensure enough rows survive the mask in the worst case (when every filtered item happens to be in the top-N scoring window). No behavior change for callers that don't use `filter_items`.
Test plan
Manual reproducer:
```python
from implicit.nearest_neighbours import CosineRecommender
from scipy.sparse import csr_matrix
import numpy as np
model = CosineRecommender(K=20)
model.fit(csr_matrix(np.random.rand(10, 10)))
user_items = csr_matrix(np.ones((1, 10)))
ids, _ = model.recommend(0, user_items, N=3, filter_items=[1, 2, 3], filter_already_liked_items=False)
assert len(ids) == 3 # before: len(ids) == 6
```
Diff is 5 lines: 1 variable assignment, 1 comment block, and a 1-character change on each of two slice lines.