Skip to content Skip to sidebar Skip to footer

Calculate Cosine Similarity Of Two Matrices

I have defined two matrices like following: from scipy import linalg, mat, dot a = mat([-0.711,0.730]) b = mat([-1.099,0.124]) Now, I want to calculate the cosine similarity of th

Solution 1:

You cannot multiply 1x2 matrix by 1x2 matrix. In order to calculate dot product between their rows the second one has to be transposed.

from scipy import linalg, mat, dot
a = mat([-0.711,0.730])
b = mat([-1.099,0.124])

c = dot(a,b.T)/linalg.norm(a)/linalg.norm(b)

Solution 2:


Post a Comment for "Calculate Cosine Similarity Of Two Matrices"