我定义了两个矩阵,如下所示:
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)
我定义了两个矩阵,如下所示:
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)
您不能将 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)
还:
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)