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?
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With