39
$\begingroup$

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?

$\endgroup$

4 Answers 4

20
$\begingroup$

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 cases where the vectors A and B are comprised 0s and 1s only, cosine similarity divides the number of common attributes by the product of A and B's distance from zero. 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.

$\endgroup$
5
  • 3
    $\begingroup$ could you please explain why is Cosine index better for identify plagiarism and not good for identifying mirror sites? $\endgroup$
    – simplfuzz
    Apr 22, 2016 at 7:09
  • $\begingroup$ 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. $\endgroup$
    – fsociety
    Jun 18, 2016 at 10:35
  • 3
    $\begingroup$ This answer is wrong about cosine similarity, please consider the answer of user18596 $\endgroup$
    – Robin
    Feb 23, 2017 at 12:24
  • 2
    $\begingroup$ "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. $\endgroup$
    – Sean Owen
    Dec 27, 2018 at 15:14
  • $\begingroup$ @saq7 why don't you remove this incorrect answer altogether? I see no point in keeping it here. $\endgroup$
    – Tim
    Aug 10, 2021 at 12:59
32
$\begingroup$

The answer from saq7 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 only the 1 bits and take the square root.

Sorry I don't have a real answer as to when you should use which metric, but I can't let the incorrect answer go unchallenged.

$\endgroup$
1
  • 2
    $\begingroup$ 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 $\endgroup$
    – neelshiv
    Nov 3, 2016 at 14:51
12
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Mar 30, 2018 at 15:14
  • $\begingroup$ cosine distance (1-cosine similarity) is a measure of dissimilarity $\endgroup$
    – Mr1159pm
    May 27, 2020 at 21:34
8
$\begingroup$

The answer by saq7 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.

$\endgroup$
2
  • $\begingroup$ This is my first time seeing cosine defined on sets. Why does intersection on sets correspond to dot product on vectors? $\endgroup$ Mar 5, 2020 at 16:58
  • $\begingroup$ Note the emphasis on binary vectors. All zeros will cancel out and the dot product will just be a sum of the overlapping 1s. $\endgroup$
    – moefasa
    Mar 7, 2020 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.