Hacker News new | past | comments | ask | show | jobs | submit login

I think the speed of your pure Python version can be improved by a factor of 2..3 by making better use of some features in the standard library. Try:

    from math import sqrt, hypot
    from operator import mul

    def cosine_distance(a, b):
        dot_product = sum(map(mul, a, b))
        magnitude_a = hypot(*a)
        magnitude_b = hypot(*b)
        cosine_similarity = dot_product / (magnitude_a * magnitude_b)
        return 1 - cosine_similarity
Also check sumprod introduced in the math module from 3.12 onwards.



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: