467,168 Members | 988 Online

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 import numpy.linalg as LA import numpy as np import sys import os f=[[3492.6], [13756.2], [22442.1], [22361.9], [26896.4]] cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 2) for c in f:      for i in f:         cosine=cx(c, i) print cosine Any ideas?, many thanks. Jul 29 '13 #1

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

• viewed: 2172
Share: