余弦相似度

Cosine Similarity

我在阅读时发现了这个公式:

该公式用于余弦相似度。我认为这看起来很有趣,因此我创建了一个 numpy 数组,其中 user_id 作为行,item_id 作为列。例如,让 M 成为这个矩阵:

M = [[2,3,4,1,0],[0,0,0,0,5],[5,4,3,0,0],[1,1,1,1,1]] 

此处矩阵中的条目是人们 u 根据行 u 和列 i 对项目 i 的评分。我想计算项目(行)之间矩阵的余弦相似度。我相信这应该会产生一个 5 x 5 的矩阵。我试着做

df = pd.DataFrame(M)
item_mean_subtracted = df.sub(df.mean(axis=0), axis=1)
similarity_matrix = item_mean_subtracted.fillna(0).corr(method="pearson").values

然而,这似乎不对。

调整余弦相似度的可能实现方式如下:

import numpy as np
from scipy.spatial.distance import pdist, squareform

M = np.asarray([[2, 3, 4, 1, 0], 
                [0, 0, 0, 0, 5], 
                [5, 4, 3, 0, 0], 
                [1, 1, 1, 1, 1]])

M_u = M.mean(axis=1)
item_mean_subtracted = M - M_u[:, None]
similarity_matrix = 1 - squareform(pdist(item_mean_subtracted.T, 'cosine'))

备注:

  • 我正在利用 NumPy broadcasting 来减去均值。
  • 如果 M 是稀疏矩阵,您可以这样做:M.toarray().
  • 来自docs

    Y = pdist(X, 'cosine')
    Computes the cosine distance between vectors u and v,
    1 − u⋅v / (||u||2||v||2)
    where ||∗||2 is the 2-norm of its argument *, and u⋅v is the dot product of u and v.

  • 数组转置是通过T方法进行的。

演示:

In [277]: M_u
Out[277]: array([ 2. ,  1. ,  2.4,  1. ])

In [278]: item_mean_subtracted
Out[278]: 
array([[ 0. ,  1. ,  2. , -1. , -2. ],
       [-1. , -1. , -1. , -1. ,  4. ],
       [ 2.6,  1.6,  0.6, -2.4, -2.4],
       [ 0. ,  0. ,  0. ,  0. ,  0. ]])

In [279]: np.set_printoptions(precision=2)

In [280]: similarity_matrix
Out[280]: 
array([[ 1.  ,  0.87,  0.4 , -0.68, -0.72],
       [ 0.87,  1.  ,  0.8 , -0.65, -0.91],
       [ 0.4 ,  0.8 ,  1.  , -0.38, -0.8 ],
       [-0.68, -0.65, -0.38,  1.  ,  0.27],
       [-0.72, -0.91, -0.8 ,  0.27,  1.  ]])