Skip to content
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