467,168 Members | 988 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,168 developers. It's quick & easy.

Computation of cosine similarity over a list of values using python

Below are my code lines for computing cosine similarity over a list of values. My goal is to compute the cosine similarity by comparing each value in the f-list( f=[[3492.6], [13756.2], [22442.1], [22361.9], [26896.4]]) with the rest values and output their similarity scores. However, for some reasons, I keep getting 1.0 as the cosine similarity even when I tested the code on other data sets. Obviously, [22361.9] is more similar to [22442.1] than [13756.2]. I just want take one value from the list and compute how close in terms of cosine distance the rest values in the list are from it. Hence the result should be five different similarity scores.

Expand|Select|Wrap|Line Numbers
  1. import numpy.linalg as LA
  2. import numpy as np
  3. import sys
  4. import os
  5. f=[[3492.6], [13756.2], [22442.1], [22361.9], [26896.4]]
  6. cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 2)
  7. for c in f:
  8.      for i in f:
  9.         cosine=cx(c, i)
  10. print cosine
Any ideas?, many thanks.
Jul 29 '13 #1

✓ answered by bvdet

Apparently the calculations np.inner(a, b) and (LA.norm(a)*LA.norm(b)) are returning the same values.

  • viewed: 2172
1 Reply
Expert Mod 2GB
Apparently the calculations np.inner(a, b) and (LA.norm(a)*LA.norm(b)) are returning the same values.
Jul 29 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Varun Hiremath | last post: by
29 posts views Thread by 63q2o4i02@sneakemail.com | last post: by
8 posts views Thread by John Salerno | last post: by
9 posts views Thread by dominiquevalentine@gmail.com | last post: by
53 posts views Thread by Vicent Giner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.