Optimized method for calculating cosine distance in Python

前端 未结 8 905
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 21:27

I wrote a method to calculate the cosine distance between two arrays:

def cosine_distance(a, b):
    if len(a) != len(b):
        return False
    numerator = 0
         


        
8条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 21:52

    Similar to Darius Bacon's answer, I've been toying with operator and itertools to produce a faster answer. The following seems to be 1/3 faster on a 500-item array according to timeit:

    from math import sqrt
    from itertools import imap
    from operator import mul
    
    def op_cosine(a, b):
        dot_prod = sum(imap(mul, a, b))
        a_veclen = sqrt(sum(i ** 2 for i in a))
        b_veclen = sqrt(sum(i ** 2 for i in b))
    
        return 1 - dot_prod / (a_veclen * b_veclen)
    

提交回复
热议问题