0
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.

  • 1
    Unindent the last two lines, i.e., `cosine_similarity = ...` and `print(...)`. – j1-lee Dec 30 '22 at 17:17
  • And don't name variable "sum" ever in Python – Gameplay Dec 30 '22 at 17:21
  • Please refer the solution given in the below link. https://stackoverflow.com/questions/18424228/cosine-similarity-between-2-number-lists. And from your code do not use "sum" as variable. Validate before performing divide operation. if all([sums, sums1, sums3]): cosine_similarity = sums / (sums1 * sums3) – Thangaselvi Dec 30 '22 at 17:26
  • just look at the product of the 1st entries of the lists, `S1[0] * S3[0]` -> `0`. Then you use the product in a division... – cards Dec 30 '22 at 17:26

1 Answers1

1

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
cards
  • 3,936
  • 1
  • 7
  • 25