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

Function to make top-k recommendations to all users in a fast way for ALS model. #179

Merged
merged 18 commits into from
Jun 20, 2019
Merged
2 changes: 1 addition & 1 deletion implicit/_nearest_neighbours.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,204 changes: 638 additions & 566 deletions implicit/evaluation.cpp

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions implicit/evaluation.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# distutils: language = c++
# cython: language_level=3

import tqdm
import numpy as np
from scipy.sparse import coo_matrix, csr_matrix
Expand Down Expand Up @@ -32,11 +35,11 @@ def train_test_split(ratings, train_percentage=0.8):
test_index = random_index >= train_percentage

train = csr_matrix((ratings.data[train_index],
(ratings.row[train_index], ratings.col[train_index])),
(ratings.row[train_index], ratings.col[train_index])),
shape=ratings.shape, dtype=ratings.dtype)

test = csr_matrix((ratings.data[test_index],
(ratings.row[test_index], ratings.col[test_index])),
(ratings.row[test_index], ratings.col[test_index])),
shape=ratings.shape, dtype=ratings.dtype)

test.data[test.data < 0] = 0
Expand All @@ -55,9 +58,11 @@ def precision_at_k(model, train_user_items, test_user_items, int K=10,
model : RecommenderBase
The fitted recommendation model to test
train_user_items : csr_matrix
Sparse matrix of user by item that contains elements that were used in training the model
Sparse matrix of user by item that contains elements that were used
in training the model
test_user_items : csr_matrix
Sparse matrix of user by item that contains withheld elements to test on
Sparse matrix of user by item that contains withheld elements to
test on
K : int
Number of items to test on
show_progress : bool, optional
Expand Down Expand Up @@ -90,7 +95,7 @@ def precision_at_k(model, train_user_items, test_user_items, int K=10,
progress = tqdm.tqdm(total=users, disable=not show_progress)

with nogil, parallel(num_threads=num_threads):
ids = <int *> malloc(sizeof(int) * K)
ids = <int * > malloc(sizeof(int) * K)
likes = new unordered_set[int]()
try:
for u in prange(users, schedule='guided'):
Expand Down Expand Up @@ -173,7 +178,7 @@ def mean_average_precision_at_k(model, train_user_items, test_user_items, int K=
progress = tqdm.tqdm(total=users, disable=not show_progress)

with nogil, parallel(num_threads=num_threads):
ids = <int *> malloc(sizeof(int) * K)
ids = <int * > malloc(sizeof(int) * K)
likes = new unordered_set[int]()
try:
for u in prange(users, schedule='guided'):
Expand Down Expand Up @@ -212,7 +217,6 @@ def mean_average_precision_at_k(model, train_user_items, test_user_items, int K=
return mean_ap / total


@cython.boundscheck(False)
def ndcg_at_k(model, train_user_items, test_user_items, int K=10,
show_progress=True, int num_threads=1):
""" Calculates ndcg@K for a given trained model
Expand Down
Loading