Home > Blockchain >  python is giving ZERODIVISIONERROR. can anyone fix this
python is giving ZERODIVISIONERROR. can anyone fix this

Time:12-31

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.

CodePudding user response:

I think you just need to remove the extra parentheses at the end of your code like this:

cosine_similarity = sum / (sqrt(sums1)) * (sqrt(sums3))
print (cosine_similarity)

CodePudding user response:

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
  • Related