Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Cosine Similarity (http://www.velocityreviews.com/forums/t955217-cosine-similarity.html)

 subhabangalore@gmail.com 12-06-2012 10:15 PM

Cosine Similarity

Dear Group,

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.

If anyone of the learned members may kindly help out.

Regards,
Subhabrata.

 Miki Tebeka 12-07-2012 04:17 AM

Re: Cosine Similarity

On Thursday, December 6, 2012 2:15:53 PM UTC-8, subhaba...@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

 subhabangalore@gmail.com 12-07-2012 04:38 PM

Re: Cosine Similarity

T

On Friday, December 7, 2012 9:47:46 AM UTC+5:30, Miki Tebeka wrote:
> On Thursday, December 6, 2012 2:15:53 PM UTC-8, subhaba...@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

Thanks Miki. It worked. Regards,Subhabrata.

 All times are GMT. The time now is 01:40 AM.