本文介绍了计算两个矩阵的余弦相似度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经定义了两个矩阵,如下所示:
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])
现在,我想计算这两个矩阵的余弦相似度.以下代码有什么问题.它给我一个objects are not aligned
Now, I want to calculate the cosine similarity of these two matrices. What is the wrong with following code. It gives me an error of objects are not aligned
c = dot(a,b)/np.linalg.norm(a)/np.linalg.norm(b)
推荐答案
您不能将1x2矩阵乘以1x2矩阵.为了计算行之间的点积,必须对第二行进行转置.
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)
这篇关于计算两个矩阵的余弦相似度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!