Fix symmetrical case for hellinger distance. Fix #1854#1860
Merged
menshikh-iv merged 3 commits intoJan 30, 2018
Conversation
This changes fix the bugs that return different distance when we call hellinger(x, y) and hellinger(y, x). The cause of this bug is that we compute the distance based on one distribution's index previously, but we should iterate all the index appears in two probability distributions.
menshikh-iv
suggested changes
Jan 26, 2018
| vec1, vec2 = dict(vec1), dict(vec2) | ||
| if len(vec2) < len(vec1): | ||
| vec1, vec2 = vec2, vec1 # swap references so that we iterate over the shorter vector | ||
| indexs = set(vec1.keys() + vec2.keys()) |
| indexs = set(vec1.keys() + vec2.keys()) | ||
| sim = np.sqrt( | ||
| 0.5 * sum((np.sqrt(value) - np.sqrt(vec2.get(index, 0.0)))**2 for index, value in iteritems(vec1)) | ||
| 0.5 * sum((np.sqrt(vec1.get(index, 0.0)) - np.sqrt(vec2.get(index, 0.0)))**2 for index in indexs) |
Contributor
There was a problem hiding this comment.
Also please add a test for it (you can use an example from original issue).
Contributor
Author
There was a problem hiding this comment.
Should I add a test in this function https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/test/test_similarity_metrics.py#L106 or anywhere else ? And it seems that the current two different length BOW inputs test in Line109 has a wrong expected value, I'm tring to correct it.
Contributor
There was a problem hiding this comment.
Yes, please fix current test & add needed tests to TestHellinger class
* fix the currrent test for different length BOW inputs * add a test for symmetrical inputs
Contributor
|
Good work @caiyulun, congratz with first contribution 👍 |
sj29-innovate
pushed a commit
to sj29-innovate/gensim
that referenced
this pull request
Feb 21, 2018
…vorky#1860) * fix: fix bugs in hellinger distance computing This changes fix the bugs that return different distance when we call hellinger(x, y) and hellinger(y, x). The cause of this bug is that we compute the distance based on one distribution's index previously, but we should iterate all the index appears in two probability distributions. * rename variable + fix union indices error in python3 * fix and add tests for hellinger distance * fix the currrent test for different length BOW inputs * add a test for symmetrical inputs
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This changes fix the bugs that return different distance when we call hellinger(x, y) and hellinger(y,x) ( #1854)
The cause of this bug is that we compute the distance based on one distribution's index previously, but we should iterate all the index appears in two probability distributions.