You need to change a couple things: first, when using dot()
, the last dimension of the first array needs to match the second-to-last dimension of the second array, so in your case what you probably want is dot(a, b.T)
. Then, in order to compute the norm of each constituent array within b
instead of computing the norm of b
as a matrix, you need norm(b, axis=1)
. Putting those together, you probably should use dot(a, b.T) / (norm(a) * norm(b, axis=1))
.
↧
Answer by David Z for How to calculate the cosine similarity betwen list to lists?
↧