Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cosine Similarity

I calculated tf/idf values of two documents. The following are the tf/idf values:

1.txt
0.0
0.5
2.txt
0.0
0.5

The documents are like:

1.txt = > dog cat
2.txt = > cat elephant

How can I use these values to calculate cosine similarity?

I know that I should calculate the dot product, then find distance and divide dot product by it. How can I calculate this using my values?

One more question: Is it important that both documents should have same number of words?

like image 726
user238384 Avatar asked Jan 04 '10 06:01

user238384


2 Answers

            a * b
sim(a,b) =--------
           |a|*|b|

a*b is dot product

some details:

def dot(a,b):
  n = length(a)
  sum = 0
  for i in xrange(n):
    sum += a[i] * b[i];
  return sum

def norm(a):
  n = length(a)
  for i in xrange(n):
    sum += a[i] * a[i]
  return math.sqrt(sum)

def cossim(a,b):
  return dot(a,b) / (norm(a) * norm(b))

yes. to some extent, a and b must have the same length. but a and b usually have sparse representation, you only need to store non-zero entries and you can calculate norm and dot more fast.

like image 198
Yin Zhu Avatar answered Sep 19 '22 01:09

Yin Zhu


simple java code implementation:

  static double cosine_similarity(Map<String, Double> v1, Map<String, Double> v2) {
            Set<String> both = Sets.newHashSet(v1.keySet());
            both.retainAll(v2.keySet());
            double sclar = 0, norm1 = 0, norm2 = 0;
            for (String k : both) sclar += v1.get(k) * v2.get(k);
            for (String k : v1.keySet()) norm1 += v1.get(k) * v1.get(k);
            for (String k : v2.keySet()) norm2 += v2.get(k) * v2.get(k);
            return sclar / Math.sqrt(norm1 * norm2);
    }
like image 20
yura Avatar answered Sep 18 '22 01:09

yura