5

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

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)
4

2 回答 2

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)
于 2014-02-24T06:55:07.570 回答
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

于 2017-05-17T07:45:21.113 回答