# Applications and differences for Jaccard similarity and Cosine Similarity

Jaccard similarity and cosine similarity are two very common measurements while comparing item similarities. However, I am not very clear in what situation which one should be preferable than another.

Can somebody help clarify the differences of these two measurements (the difference in concept or principle, not the definition or computation) and their preferable applications?

Jaccard Similarity is given by $s_{ij} = \frac{p}{p+q+r}$

where,

p = # of attributes positive for both objects
q = # of attributes 1 for i and 0 for j
r = # of attributes 0 for i and 1 for j

Whereas, cosine similarity = $\frac{A \cdot B}{\|A\|\|B\|}$ where A and B are object vectors.

Simply put, in cosine similarity, the number of common attributes is divided by the total number of possible attributes. Whereas in Jaccard Similarity, the number of common attributes is divided by the number of attributes that exists in at least one of the two objects.

And there are many other measures of similarity, each with its own eccentricities. When deciding which one to use, try to think of a few representative cases and work out which index would give the most usable results to achieve your objective.

The Cosine index could be used to identify plagiarism, but will not be a good index to identify mirror sites on the internet. Whereas the Jaccard index, will be a good index to identify mirror sites, but not so great at catching copy pasta plagiarism (within a larger document).

When applying these indices, you must think about your problem thoroughly and figure out how to define similarity. Once you have a definition in mind, you can go about shopping for an index.

Edit: Earlier, I had an example included in this answer, which was ultimately incorrect. Thanks to the several users who have pointed that out, I have removed the erroneous example.

• could you please explain why is Cosine index better for identify plagiarism and not good for identifying mirror sites? – dharm0us Apr 22 '16 at 7:09
• I feel like some parts of this answer are non-intuitive. "For example, if you have two objects both with 10 attributes, out of a possible 100 attributes. Further they have all 10 attributes in common. In this case, the Jaccard index will be 1 and the cosine index will be 0.001." This would translate to something like cosine_similarity(10*[1]+90*[0], 10*[1]+90*[0]). Of course, the cosine similarity would also be 1 here, as both measure ignore those elements that are zero in both vectors. – fsociety Jun 18 '16 at 10:35
• "Simply put, in cosine similarity, the number of common attributes is divided by the total number of possible attributes" -> this is entirely incorrect. The notation defines vector dot products and norms. – Sean Owen Dec 27 '18 at 15:14

I can't comment because I don't have any status, but the checked answer is wrong, as well as not answering the question. ∥A∥ means the L2 norm of A, i.e. the length of the vector in Euclidean space, not the dimensionality of the vector A. In other words, you don't count the 0 bits, you add up the 1 bits and take the square root. So the example of 10 attributes of a 100-length vector is wrong, too. Sorry I don't have a real answer as to when you should use which metric, but I can't just let the incorrect answer go unchallenged.

• You're totally right. It's a shame that so many people are voting up an incorrect answer. Cosine similarity, as described in the wikipedia article, does not take into account 0 bits. en.wikipedia.org/wiki/Cosine_similarity – neelshiv Nov 3 '16 at 14:51

Jaccard similarity is used for two types of binary cases:

1. Symmetric, where 1 and 0 has equal importance (gender, marital status,etc)
2. Asymmetric, where 1 and 0 have different levels of importance (testing positive for a disease)

Cosine similarity is usually used in the context of text mining for comparing documents or emails. If the cosine similarity between two document term vectors is higher, then both the documents have more number of words in common

Another difference is 1 - Jaccard Coefficient can be used as a dissimilarity or distance measure, whereas the cosine similarity has no such constructs. A similar thing is the Tonimoto distance, which is used in taxonomy.

• Why is it that only Jaccard can be used as a dissimilarity measure? My understanding is that cosine is a different but not invalid measure. – javadba Mar 30 '18 at 15:14

As noted above, the checked answer is wrong.

Where $$\mathbf{a}$$ and $$\mathbf{b}$$ are binary vectors, they can be interpreted as sets of indices with value 1. Let's therefore consider sets $$A$$ and $$B$$.

Jaccard similarity is then given by $$J(A, B) = \frac{|A \cap B|}{|A \cup B|} = \frac{|A \cap B|}{|A \cap B| + |A - B| + |B - A|}$$

Cosine similarity is then given by $$C(A, B) = \frac{|A \cap B|}{\sqrt{\left|A\right|\left|B\right|}} = \frac{|A \cap B|}{\sqrt{(\left|A\cap B\right| + |A - B|)(\left|A\cap B\right| + |B - A|)}}$$

Some comparisons:

• The numerators here are the same.
• The denominator grows arithmetically with the size of $$|A|$$ and $$|B|$$ in jaccard, but geometrically in cosine.
• The denominator of cosine depends only on the number of items in $$|A|$$ and the number of items in $$|B|$$. It does not depend on their intersection.

I do not yet have a clear intuition on where one should be preferred over the other, except that, as Vikram Venkat noted, 1 - Jaccard corresponds to a true metric, unlike cosine; and cosine naturally extends to real-valued vectors.