计算两个矩阵的余弦相似度 - Python

时间:2014-02-24 06:42:56

标签: python numpy matrix cosine-similarity

我已经定义了两个矩阵,如下所示:

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

现在,我想计算这两个矩阵的余弦相似度。以下代码有什么问题。它给我一个错误objects are not aligned

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

2 个答案:

答案 0 :(得分:8)

您不能将1x2矩阵乘以1x2矩阵。为了计算它们的行之间的点积,第二个必须被转置。

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)

答案 1 :(得分:6)

还:

import numpy as np
import scipy.spatial.distance as distance
a = np.array([0.1, 0.2])
b = np.array([0.3,0.4])
c = 1 - distance.cosine(a, b)

请参阅:https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cosine.html#scipy.spatial.distance.cosine