Skip to content

Commit

Permalink
Merge pull request #1 from Tych0n/travis_ci
Browse files Browse the repository at this point in the history
PEP8ified
  • Loading branch information
Tych0n authored Jan 15, 2019
2 parents ca168b4 + a56193c commit 62d0323
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
37 changes: 18 additions & 19 deletions implicit/evaluation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from libcpp.unordered_set cimport unordered_set

from math import ceil
cdef extern from "topnc.h":
cdef void fargsort_c ( float A[], int n_row, int m_row, int m_cols, int ktop, int B[] ) nogil;
cdef void fargsort_c(float A[], int n_row, int m_row, int m_cols, int ktop, int B[]) nogil


def train_test_split(ratings, train_percentage=0.8):
Expand All @@ -39,11 +39,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 @@ -62,9 +62,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 @@ -97,7 +99,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 @@ -180,7 +182,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 @@ -223,25 +225,22 @@ def mean_average_precision_at_k(model, train_user_items, test_user_items, int K=
@cython.wraparound(False)
@cython.nonecheck(False)
def ALS_recommend_all(
model
, users_items
, int k=10
, int threads=1
, show_progress=True
, recalculate_user=False):
model, users_items, int k=10, int threads=1, show_progress=True, recalculate_user=False):

if not isinstance(users_items, csr_matrix):
users_items = users_items.tocsr()
factors_items = model.item_factors.T

cdef int users_c = users_items.shape[0], items_c = users_items.shape[1], u_b, u_low, u_high, u_len, u, batch = threads * 10
cdef:
int users_c = users_items.shape[0], items_c = users_items.shape[1], batch = threads * 10
int u_b, u_low, u_high, u_len, u
A = np.zeros((batch, items_c), dtype=np.float32)
cdef:
int users_c_b = ceil(users_c / batch)
float[:,::1] A_mv = A
float* A_mv_p = &A_mv[0,0]
int[:,::1] B_mv = np.zeros((users_c, k), dtype=np.intc)
int* B_mv_p = &B_mv[0,0]
float[:, ::1] A_mv = A
float * A_mv_p = &A_mv[0, 0]
int[:, ::1] B_mv = np.zeros((users_c, k), dtype=np.intc)
int * B_mv_p = &B_mv[0, 0]

progress = tqdm.tqdm(total=users_c, disable=not show_progress)
for u_b in range(users_c_b):
Expand All @@ -258,4 +257,4 @@ def ALS_recommend_all(
fargsort_c(A_mv_p, u, batch * u_b + u, items_c, k, B_mv_p)
progress.update(u_len)
progress.close()
return np.asarray(B_mv)
return np.asarray(B_mv)
2 changes: 1 addition & 1 deletion implicit/topnc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ void fargsort_c ( float A[], int n_row, int m_row, int m_cols, int ktop, int B[]
for ( int j = 0; j < ktop; j++ ) {
B[(m_row*ktop) + j] = targets[j].index;
}
}
}
9 changes: 5 additions & 4 deletions implicit/topnc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef UTILS_CPPCLASS_H
#define UTILS_CPPCLASS_H
/* "Copyright [2019] <Tych0n>" [legal/copyright] */
#ifndef IMPLICIT_TOPNC_H_
#define IMPLICIT_TOPNC_H_

extern void fargsort_c ( float A[], int n_row, int m_row, int m_cols, int ktop, int B[] );
extern void fargsort_c(float A[], int n_row, int m_row, int m_cols, int ktop, int B[]);

#endif //UTILS_CPPCLASS_H
#endif // IMPLICIT_TOPNC_H_
10 changes: 4 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ def define_extensions(use_cython=False):
extra_compile_args=compile_args, extra_link_args=link_args)
for name in ['_als', '_nearest_neighbours', 'bpr']]
modules.append(Extension("implicit." + 'evaluation',
[
os.path.join("implicit", 'evaluation' + src_ext)
, os.path.join("implicit", 'topnc.cpp')
],
language='c++',
extra_compile_args=compile_args, extra_link_args=link_args))
[os.path.join("implicit", 'evaluation' + src_ext),
os.path.join("implicit", 'topnc.cpp')],
language='c++',
extra_compile_args=compile_args, extra_link_args=link_args))

if CUDA:
modules.append(Extension("implicit.cuda._cuda",
Expand Down

0 comments on commit 62d0323

Please sign in to comment.