Reputation: 9
from math import sqrt
S1 = [1,0,0,0,1,0,0,2]
S3 = [0,1,1,2,0,1,2,0]
sum = 0
sums1 = 0
sums3 = 0
for i, j in zip(S1,S3):
sums1 += i*i
sums3 += j*j
sum += i*j
cosine_similarity = sum / ((sqrt(sums1)) * (sqrt(sums3)))
print (cosine_similarity)
plz how can I remove this error from code. I want to find cosine similarity of vectors.
Upvotes: 0
Views: 59
Reputation: 4975
The error is due to the indentation level of the last two lines (as mentioned by in the comments by j1-lee):
# ...
sum += i*j
# deindentation
cosine_similarity = sum / ((sqrt(sums1)) * (sqrt(sums3)))
print (cosine_similarity)
Here another implementation by decomposing the definition of cosine similarity into smaller operations:
def scalar_product(a, b):
return sum(a_i*b_i for a_i, b_i in zip(a, b))
def norm(a):
return sum(a_i**2 for a_i in a )**.5
def cosine_similarity(a, b):
return scalar_product(a, b) / (norm(a)*norm(b))
S1 = [1,0,0,0,1,0,0,2]
S3 = [0,1,1,2,0,1,2,0]
cs = cosine_similarity(S1, S3)
print(cs)
# 0.0 # orthogonality
cs = cosine_similarity(S1, S1)
print(cs)
# 1.0...# parallelity
Upvotes: 1