-
-
Save veekaybee/32b121eceaa1dbfb6a268ea544893bd0 to your computer and use it in GitHub Desktop.
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
v1 = [0,3,4,5,6] | |
v2 = [4,5,6,7,8] | |
def dot(v1, v2): | |
dot_product = sum((a * b) for a,b in zip(v1,v2)) | |
return dot_product | |
def cosine_similarity(v1, v2): | |
''' | |
(v1 dot v2)/||v1|| *||v2||) | |
''' | |
products = dot(v1,v2) | |
denominator = ( (dot(v1,v1) **.5) * (dot(v2,v2) ** .5) ) | |
similarity = products / denominator | |
return similarity | |
print(cosine_similarity(v1, v2)) | |
# 0.9544074144996451 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment