Cosine Similarity

Discussion in 'Python' started by subhabangalore@gmail.com, Dec 6, 2012.

  1. Guest

    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.
     
    , Dec 6, 2012
    #1
    1. Advertising

  2. Miki Tebeka Guest

    On Thursday, December 6, 2012 2:15:53 PM UTC-8, 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
     
    Miki Tebeka, Dec 7, 2012
    #2
    1. Advertising

  3. Guest

    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, 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.
     
    , Dec 7, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niv

    cosine calcs

    Niv, Aug 11, 2006, in forum: VHDL
    Replies:
    9
    Views:
    2,097
    Jonathan Bromley
    Aug 16, 2006
  2. pete

    Cosine algo, was Re: sqrt algo

    pete, Oct 15, 2005, in forum: C Programming
    Replies:
    108
    Views:
    1,737
  3. Replies:
    1
    Views:
    403
    mlimber
    Mar 24, 2006
  4. FPGA
    Replies:
    8
    Views:
    1,292
  5. FPGA
    Replies:
    15
    Views:
    1,184
    Brian Drummond
    Feb 9, 2008
Loading...

Share This Page