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
This is faster for arrays of around 1000+ elements.
from numpy import array def cosine_distance(a, b): a=array(a) b=array(b) numerator=(a*b).sum() denoma=(a*a).sum() denomb=(b*b).sum() result = 1 - numerator / sqrt(denoma*denomb) return result