[英]create a function to compute all pairwise cosine similarity of the row vectors in a 2-D matrix using only numpy
例如,给定矩阵
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
它应该返回
array([[1. , 0.91465912, 0.87845859],
[0.91465912, 1. , 0.99663684],
[0.87845859, 0.99663684, 1. ]])
其中结果的(i, j)
项是行向量arr[i]
和行向量arr[j]
之间的余弦相似度: cos_sim[i, j] == CosSim(arr[i], arr[j])
。
这个 function 应该返回一个 np.ndarray 形状 (arr.shape[0], arr.shape[0])
尝试:
from scipy.spatial.distance import cdist
1 - cdist(a, a, metric='cosine')
Output:
array([[1. , 0.91465912, 0.87845859],
[0.91465912, 1. , 0.99663684],
[0.87845859, 0.99663684, 1. ]])
a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
p = a / np.linalg.norm(a, 2, axis=1).reshape(-1,1)
p
array([[0. , 0.18257419, 0.36514837, 0.54772256, 0.73029674],
[0.31311215, 0.37573457, 0.438357 , 0.50097943, 0.56360186],
[0.37011661, 0.40712827, 0.44413993, 0.48115159, 0.51816325]])
请注意,范数必须逐行计算。 所以,我们有上面的axis=1
。 此外,规范将是 1 级向量。 因此,在这种情况下,要转换为形状(3,1)
,就需要 reshape。 另外,上面的公式是针对向量的,当你应用到矩阵时,“转置部分会排在第二位”。
现在在这种情况下,q 只不过是 piteslf。 所以,余弦相似度将是
np.dot(p, p.T)
array([[1. , 0.91465912, 0.87845859],
[0.91465912, 1. , 0.99663684],
[0.87845859, 0.99663684, 1. ]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.