-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Cosine full vector implementation #141
Open
ODemidenko
wants to merge
1
commit into
NicolasHug:master
Choose a base branch
from
ODemidenko:cosine_full_vector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,11 @@ | |
|
||
n_x = 8 | ||
yr_global = { | ||
0: [(0, 3), (1, 3), (2, 3), (5, 1), (6, 1.5), (7, 3)], # noqa | ||
0: [(0, 3), (1, 3), (2, 3), (5, 1), (6, 1.5), (7, 3)], # noqa | ||
1: [(0, 4), (1, 4), (2, 4), ], # noqa | ||
2: [ (2, 5), (3, 2), (4, 3) ], # noqa | ||
3: [(1, 1), (2, 4), (3, 2), (4, 3), (5, 3), (6, 3.5), (7, 2)], # noqa | ||
4: [(1, 5), (2, 1), (5, 2), (6, 2.5), (7, 2.5)], # noqa | ||
3: [ (1, 1), (2, 4), (3, 2), (4, 3), (5, 3), (6, 3.5), (7, 2)], # noqa | ||
4: [ (1, 5), (2, 1), (5, 2), (6, 2.5), (7, 2.5)], # noqa | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
} | ||
|
||
|
||
|
@@ -48,7 +48,51 @@ def test_cosine_sim(): | |
# cosine sim is necessarily 1 | ||
assert sim[3, 4] == 1 | ||
|
||
# pairs of users (0, 3) have no common items | ||
# pairs of users (0, 3) have no common items | ||
assert sim[0, 3] == 0 | ||
assert sim[0, 4] == 0 | ||
|
||
# check for float point support and computation correctness | ||
dot_product56 = 1 * 1.5 + 3 * 3.5 + 2 * 2.5 | ||
assert sim[5, 6] == (dot_product56 / | ||
((1 ** 2 + 3 ** 2 + 2 ** 2) * | ||
(1.5 ** 2 + 3.5 ** 2 + 2.5 ** 2)) ** 0.5 | ||
) | ||
|
||
# ensure min_support is taken into account. Only users 1 and 2 have more | ||
# than 4 common ratings. | ||
sim = sims.cosine(n_x, yr, min_support=4) | ||
for i in range(n_x): | ||
for j in range(i + 1, n_x): | ||
if i != 1 and j != 2: | ||
assert sim[i, j] == 0 | ||
|
||
|
||
def test_cosine_full_vectors_sim(): | ||
"""Tests for the cosine similarity.""" | ||
|
||
yr = yr_global.copy() | ||
|
||
# # shuffle every rating list, to ensure the order in which ratings are | ||
# # processed does not matter (it's important because it used to be error | ||
# # prone when we were using itertools.combinations) | ||
# for _, ratings in yr.items(): | ||
# random.shuffle(ratings) | ||
|
||
sim = sims.cosine(n_x, yr, min_support=1, common_ratings_only=False) | ||
|
||
# check symetry and bounds (as ratings are > 0, cosine sim must be >= 0) | ||
for xi in range(n_x): | ||
assert sim[xi, xi] == 1 | ||
for xj in range(n_x): | ||
assert sim[xi, xj] == sim[xj, xi] | ||
assert 0 <= sim[xi, xj] <= 1 | ||
|
||
# users 0, 1 and 2 have different ratings when non-common items considered | ||
assert sim[0, 1] < 1 | ||
assert sim[0, 2] < 1 | ||
|
||
# pairs of users (0, 3) and (0,4) have no common items | ||
assert sim[0, 3] == 0 | ||
assert sim[0, 4] == 0 | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could all the StopIteration be avoided?