-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Misc fixes across multiple algorithms #6912
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
Changes from 6 commits
61547f4
8f3fa34
424ba28
0d115a6
7d1b26d
a9cf143
8fe7311
2bb4238
4a7189b
ed0c724
1f2366d
1ba8b35
a037145
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
""" | ||
|
||
|
||
def jaccard_similariy(setA, setB, alternativeUnion=False): | ||
def jaccard_similarity(setA, setB, alternativeUnion=False): | ||
""" | ||
Finds the jaccard similarity between two sets. | ||
Essentially, its intersection over union. | ||
|
@@ -35,21 +35,24 @@ def jaccard_similariy(setA, setB, alternativeUnion=False): | |
Examples: | ||
>>> setA = {'a', 'b', 'c', 'd', 'e'} | ||
>>> setB = {'c', 'd', 'e', 'f', 'h', 'i'} | ||
>>> jaccard_similariy(setA,setB) | ||
>>> jaccard_similarity(setA,setB) | ||
0.375 | ||
|
||
>>> jaccard_similariy(setA,setA) | ||
>>> jaccard_similarity(setA,setA) | ||
1.0 | ||
|
||
>>> jaccard_similariy(setA,setA,True) | ||
>>> jaccard_similarity(setA,setA,True) | ||
0.5 | ||
|
||
>>> setA = ['a', 'b', 'c', 'd', 'e'] | ||
>>> setB = ('c', 'd', 'e', 'f', 'h', 'i') | ||
>>> jaccard_similariy(setA,setB) | ||
>>> jaccard_similarity(setA,setB) | ||
0.375 | ||
""" | ||
|
||
if len(setA) == 0 or len(setB) == 0: | ||
raise ValueError("Input must not be empty") | ||
|
||
if isinstance(setA, set) and isinstance(setB, set): | ||
|
||
intersection = len(setA.intersection(setB)) | ||
|
@@ -67,14 +70,14 @@ def jaccard_similariy(setA, setB, alternativeUnion=False): | |
|
||
if alternativeUnion: | ||
union = len(setA) + len(setB) | ||
return len(intersection) / union | ||
else: | ||
union = setA + [element for element in setB if element not in setA] | ||
|
||
return len(intersection) / len(union) | ||
return len(intersection) / len(union) | ||
|
||
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. This is verified, right? 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. You mean if it passes the tests? 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. No, I mean the tests do go through those branches. Earlier, there was just one return statement return len(intersection) / len(union) But, now the first return statement is different: return len(intersection) / union |
||
|
||
if __name__ == "__main__": | ||
|
||
setA = {"a", "b", "c", "d", "e"} | ||
setB = {"c", "d", "e", "f", "h", "i"} | ||
print(jaccard_similariy(setA, setB)) | ||
print(jaccard_similarity(setA, setB)) |
Uh oh!
There was an error while loading. Please reload this page.