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.
I'm trying to test multiple recommendation algorithms with a very big dataset (6M ratings, 69k users, 17k items) and I noticed that for algorithms that compute similarities the
fit
time is very high.I found that there are some redundant operations that could be eliminated in order to speed up the similarities' computation.
In
similarities.pyx
when computing auxiliary values, like the freq, prods arrays, becausey_ratings
is not sorted we have to fully iterate overy_ratings
two times resulting in N² operations for eachy_ratings
inyr
.We can take advantage of the fact that all auxiliary matrices are symmetric (M[x][y] == M[y][X]) so filling the half above diagonal is enough to compute the similarities.
My change is sorting the
y_ratings
lists before filling the auxiliary arrays and changing the second for loop ony_ratings
so that only the top half of the auxiliary matrices are filled. This reduces the number of operations for each element inyr
from N² to N(N-1)/2.The sorting might reduce performance for small datasets, but for larger datasets like movielens-1m the performance gains are very noticeable.
Some performance tests I did on my laptop (Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz). The time in the next tables are the average time it took to train (just the
.fit
method call) a KNNBasic algorithm over 50 samples.Dataset: movielens-1m
Dataset: movielens-100k
Of course, all tests pass successfully.