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

前端 未结 10 1877
别跟我提以往
别跟我提以往 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:38

    This Python code is my quick and dirty attempt to implement the algorithm:

    import math
    from collections import Counter
    
    def build_vector(iterable1, iterable2):
        counter1 = Counter(iterable1)
        counter2 = Counter(iterable2)
        all_items = set(counter1.keys()).union(set(counter2.keys()))
        vector1 = [counter1[k] for k in all_items]
        vector2 = [counter2[k] for k in all_items]
        return vector1, vector2
    
    def cosim(v1, v2):
        dot_product = sum(n1 * n2 for n1, n2 in zip(v1, v2) )
        magnitude1 = math.sqrt(sum(n ** 2 for n in v1))
        magnitude2 = math.sqrt(sum(n ** 2 for n in v2))
        return dot_product / (magnitude1 * magnitude2)
    
    
    l1 = "Julie loves me more than Linda loves me".split()
    l2 = "Jane likes me more than Julie loves me or".split()
    
    
    v1, v2 = build_vector(l1, l2)
    print(cosim(v1, v2))
    

提交回复
热议问题