Cosine Similarity

Miki Tebeka miki.tebeka at gmail.com
Fri Dec 7 05:17:46 CET 2012


On Thursday, December 6, 2012 2:15:53 PM UTC-8, subhaba... at gmail.com wrote:
> I am looking for some example of implementing Cosine similarity in python. I searched for hours but could not help much. NLTK seems to have a module but did not find examples. 
Should be easy with numpy:
    import numpy as np

    def cos(v1, v2):
       return np.dot(v1, v2) / (np.sqrt(np.dot(v1, v1)) * np.sqrt(np.dot(v2, v2)))


HTH,
--
Miki



More information about the Python-list mailing list