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.
Any ideas?, many thanks.
Code:
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
Comment