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

Fix incorrect test set values in leave_k_out splits with sparse user rows #640

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion implicit/evaluation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@ cpdef leave_k_out_split(
candidate_items = items[full_candidate_mask]
candidate_data = data[full_candidate_mask]

# reindex candidate_user indices so they are properly formatted for the
# calculations in _take_tails
xsorted = np.argsort(unique_candidate_users)
reindexed_candidate_users = np.searchsorted(unique_candidate_users[xsorted], candidate_users)

test_idx, train_idx = _take_tails(
candidate_users, K, shuffled=True, return_complement=True
reindexed_candidate_users, K, shuffled=True, return_complement=True
)

# get all remaining remaining candidate user-item pairs, and prepare to append to
Expand Down
21 changes: 20 additions & 1 deletion tests/evaluation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ def _get_sample_matrix():


def _get_matrix():
mat = random(100, 100, density=0.5, format="csr", dtype=np.float32)
mat = random(100, 100, density=0.1, format="csr", dtype=np.float32)
return mat.tocoo()


def _get_fixed_matrix():
mat = csr_matrix(
[
[1, 0, 0, 0],
[3, 2, 1, 0],
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 1, 1, 1],
[0, 0, 1, 0],
]
)
return mat.tocoo()


Expand Down Expand Up @@ -48,6 +63,10 @@ def test_leave_k_out_outputs_produce_input():
train, test = leave_k_out_split(mat, K=1)
assert ((train + test) - mat).nnz == 0

mat = _get_fixed_matrix()
train, test = leave_k_out_split(mat, K=1)
assert ((train + test) - mat).nnz == 0


def test_leave_k_split_is_reservable():
"""
Expand Down