3 ms·
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
by Qem 3y ago
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.