Tiger1 0 Newbie Poster

Below are my code lines for computing cosine similarity over a list of values. My goal is to compute cosine similarity for each value in the f-list (f=[[3492.6], [13756.2], [22442.1], [22361.9], [26896.4]]) by taking a 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. However, for some reason, 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] (with respect to distance). See code below;

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 in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.