Can someone give an example of cosine similarity, in a very simple, graphical way?

前端 未结 10 1857
别跟我提以往
别跟我提以往 2020-11-28 17:04

Cosine Similarity article on Wikipedia

Can you show the vectors here (in a list or something) and then do the math, and let us see how it works?

I\'m a begin

10条回答
  •  醉梦人生
    2020-11-28 17:50

    This is a simple Python code which implements cosine similarity.

    from scipy import linalg, mat, dot
    import numpy as np
    
    In [12]: matrix = mat( [[2, 1, 0, 2, 0, 1, 1, 1],[2, 1, 1, 1, 1, 0, 1, 1]] )
    
    In [13]: matrix
    Out[13]: 
    matrix([[2, 1, 0, 2, 0, 1, 1, 1],
            [2, 1, 1, 1, 1, 0, 1, 1]])
    In [14]: dot(matrix[0],matrix[1].T)/np.linalg.norm(matrix[0])/np.linalg.norm(matrix[1])
    Out[14]: matrix([[ 0.82158384]])
    

提交回复
热议问题