【问题标题】:How to find cosine similarity of one vector vs matrix如何找到一个向量与矩阵的余弦相似度
【发布时间】:2020-12-20 17:02:35
【问题描述】:

我有一个形状为 (149,1001) 的 TF-IDF 矩阵。想要的是计算最后一列与所有列的余弦相似度

这就是我所做的

from numpy import dot
from numpy.linalg import norm
for i in range(mat.shape[1]-1):
    cos_sim = dot(mat[:,i], mat[:,-1])/(norm(mat[:,i])*norm(mat[:,-1]))
    cos_sim

但是这个循环让它变慢了。那么,有什么有效的方法吗?我只想用 numpy 做

【问题讨论】:

    标签: python numpy cosine-similarity


    【解决方案1】:

    有一个 sklearn 函数来计算向量之间的余弦相似度,cosine_similarity。这是一个带有示例数组的用例:

    a = np.random.randint(0,10,(5,5))
    print(a)
    array([[5, 2, 0, 4, 1],
           [4, 2, 8, 2, 4],
           [9, 7, 4, 9, 7],
           [4, 6, 0, 1, 3],
           [1, 1, 2, 5, 0]])
    
    from sklearn.metrics.pairwise import cosine_similarity
    cosine_similarity(a[None,:,-1] , a.T[:-1])
    # array([[0.94022805, 0.91705665, 0.75592895, 0.79921221, 1.        ]])
    

    其中a[None,-1]a 中的最后一列,重新整形后两个矩阵的Mat.shape[1] 形状相同,这是函数的要求:

    a[None,:,-1]
    # array([[1, 4, 7, 3, 0]])
    

    通过转置,结果将是 cosine_similarity 与所有其他列。

    检查问题的解决方案:

    from numpy import dot
    from numpy.linalg import norm
    cos_sim = []
    for i in range(a.shape[1]-1):
        cos_sim.append(dot(a[:,i], a[:,-1])/(norm(a[:,i])*norm(a[:,-1])))
    
    np.allclose(cos_sim, cosine_similarity(a[None,:,-1] , a.T[:-1]))
    # True
    

    【讨论】:

      【解决方案2】:

      利用2D矢量化matrix-multiplication

      这是一个使用 NumPy 对 2D 数据使用矩阵乘法的方法 -

      p1 = mat[:,-1].dot(mat[:,:-1])
      p2 = norm(mat[:,:-1],axis=0)*norm(mat[:,-1])
      out1 = p1/p2
      

      解释: p1dot(mat[:,i], mat[:,-1]) 循环的向量化等效项。 p2 属于 (norm(mat[:,i])*norm(mat[:,-1]))

      用于验证的示例运行 -

      In [57]: np.random.seed(0)
          ...: mat = np.random.rand(149,1001)
      
      In [58]: out = np.empty(mat.shape[1]-1)
          ...: for i in range(mat.shape[1]-1):
          ...:     out[i] = dot(mat[:,i], mat[:,-1])/(norm(mat[:,i])*norm(mat[:,-1]))
      
      In [59]: p1 = mat[:,-1].dot(mat[:,:-1])
          ...: p2 = norm(mat[:,:-1],axis=0)*norm(mat[:,-1])
          ...: out1 = p1/p2
      
      In [60]: np.allclose(out, out1)
      Out[60]: True
      

      时间安排 -

      In [61]: %%timeit
          ...: out = np.empty(mat.shape[1]-1)
          ...: for i in range(mat.shape[1]-1):
          ...:     out[i] = dot(mat[:,i], mat[:,-1])/(norm(mat[:,i])*norm(mat[:,-1]))
      18.5 ms ± 977 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
      
      In [62]: %%timeit   
          ...: p1 = mat[:,-1].dot(mat[:,:-1])
          ...: p2 = norm(mat[:,:-1],axis=0)*norm(mat[:,-1])
          ...: out1 = p1/p2
      939 µs ± 29.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
      
      # @yatu's soln
      In [89]: a = mat
      
      In [90]: %timeit cosine_similarity(a[None,:,-1] , a.T[:-1])
      2.47 ms ± 461 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
      

      normeinsum 上进一步优化

      或者,我们可以用np.einsum 计算p2

      所以,norm(mat[:,:-1],axis=0) 可以替换为:

      np.sqrt(np.einsum('ij,ij->j',mat[:,:-1],mat[:,:-1]))
      

      因此,给我们一个修改后的p2

      p2 = np.sqrt(np.einsum('ij,ij->j',mat[:,:-1],mat[:,:-1]))*norm(mat[:,-1])
      

      时间设置与之前相同 -

      In [82]: %%timeit
          ...: p1 = mat[:,-1].dot(mat[:,:-1])
          ...: p2 = np.sqrt(np.einsum('ij,ij->j',mat[:,:-1],mat[:,:-1]))*norm(mat[:,-1])
          ...: out1 = p1/p2
      607 µs ± 132 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
      

      30x+ 比循环的加速!

      【讨论】:

        猜你喜欢
        • 2019-05-31
        • 1970-01-01
        • 2015-07-17
        • 2019-12-23
        • 2014-11-16
        • 2021-08-20
        • 2016-09-08
        • 2020-10-28
        • 2014-03-25
        相关资源
        最近更新 更多