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. Advertisements

  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. Advertisements

  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. Advertisements

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. Fabian Leitritz

    Document-Document similarity

    Fabian Leitritz, Jan 14, 2005, in forum: Java
    Replies:
    0
    Views:
    550
    Fabian Leitritz
    Jan 14, 2005
  2. =?iso-8859-1?B?bW9vcJk=?=

    What are the similarity and difference b/w EBJ and COM+?

    =?iso-8859-1?B?bW9vcJk=?=, May 30, 2006, in forum: Java
    Replies:
    1
    Views:
    536
    dimitar
    May 30, 2006
  3. Niv

    cosine calcs

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

    Cosine algo, was Re: sqrt algo

    pete, Oct 15, 2005, in forum: C Programming
    Replies:
    108
    Views:
    2,270
  5. Replies:
    1
    Views:
    582
    mlimber
    Mar 24, 2006
  6. FPGA
    Replies:
    8
    Views:
    1,609
  7. FPGA
    Replies:
    15
    Views:
    1,473
    Brian Drummond
    Feb 9, 2008
  8. Replies:
    11
    Views:
    771
Loading...