Jaccard vs Cosine Similarity: Which Should We Use?

Jaccard similarity compares which items two objects share, while cosine similarity compares the angle between their vectors. Worked examples show when to use each.
Statistics
Programming
Explainer
Author

Abdullah Al Mahmud

Published

December 1, 2026

Left: two coinciding circles holding the words regression and residuals, with Jaccard similarity 1.00. Right: vectors u (2, 2) and v (1, 4) separated by about 31 degrees, with cosine similarity 0.86.

The same two notes look identical as sets of words, but not as vectors of word counts.

Jaccard similarity compares set overlap, while cosine similarity compares the direction of two vectors. The first asks which items are shared; the second asks whether the weights are in similar proportions.

The two formulas

For two sets \(A\) and \(B\), Jaccard similarity is the intersection over the union:

\[ J(A, B) = \frac{|A \cap B|}{|A \cup B|} \]

For two non-zero vectors \(\mathbf{u}\) and \(\mathbf{v}\), cosine similarity is the cosine of the angle \(\theta\) between them:

\[ \cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}} {\lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert} \]

Side by side

Feature Jaccard Cosine
Input Sets or 0/1 vectors Numeric vectors (counts, TF-IDF, embeddings)
Counts and weights Ignored Used
Effect of size Extra unshared items lower it Multiplying a vector by a positive number changes nothing
Range 0 to 1 −1 to 1 (0 to 1 if no entry is negative)

Example 1: counts matter

Consider three short notes. We record how often each uses the words regression and residuals:

Note regression residuals Vector
1 2 2 \((2, 2)\)
2 1 1 \((1, 1)\)
3 1 4 \((1, 4)\)

Jaccard similarity ignores the counts, so each note becomes the same set of words:

\[ A = \{\text{regression, residuals}\} \]

We compare two notes at a time, and we count words, not notes. Take Notes 1 and 2. They share 2 words, and together they use 2 distinct words:

\[ J = \frac{\text{shared words}}{\text{distinct words}} = \frac{2}{2} = 1 \]

Having three notes does not make this \(3/3\). The pairs (1, 3) and (2, 3) also give \(2/2 = 1\), since every note uses the same two words.

Notes 1 and 2 differ in length but not in mix, and cosine similarity agrees that they match:

\[ \cos\theta = \frac{2(1) + 2(1)}{\sqrt{8}\,\sqrt{2}} = \frac{4}{4} = 1 \]

Notes 1 and 3 differ in mix, since Note 3 stresses residuals:

\[ \begin{aligned} \cos\theta &= \frac{2(1) + 2(4)}{\sqrt{8}\,\sqrt{17}} \\[4pt] &= \frac{10}{\sqrt{136}} \approx 0.857 \end{aligned} \]

Jaccard cannot see this shift, but cosine can. This is why cosine similarity is common in information retrieval, document clustering and semantic search, where term weights and document length vary.

Example 2: only membership matters

Two users of a reading platform tag their books:

  • User A: statistics, bayesian, r-package
  • User B: statistics, bayesian, time-series, forecasting, python

Each tag is either present or absent. They share 2 tags out of 6 distinct ones:

\[ J = \frac{2}{6} \approx 0.333 \]

For comparison, we can code each user as a 0/1 vector, with one entry per distinct tag. In the last column we multiply the two entries:

Tag A B A × B
statistics 1 1 1
bayesian 1 1 1
r-package 1 0 0
time-series 0 1 0
forecasting 0 1 0
python 0 1 0
Total 3 5 2

The numerator of \(\cos\theta\) is the dot product, the total of the last column:

\[ \begin{aligned} \mathbf{u} \cdot \mathbf{v} &= 1 + 1 + 0 + 0 + 0 + 0 \\ &= 2 \end{aligned} \]

A product is 1 only when both users have the tag, so the numerator is simply the number of shared tags.

In the denominator, squaring a 0 or a 1 leaves it unchanged. The lengths of the vectors are therefore \(\sqrt{3}\) and \(\sqrt{5}\):

\[ \cos\theta = \frac{2}{\sqrt{3}\,\sqrt{5}} \approx 0.516 \]

Here Jaccard is the natural choice: it reads directly as the share of all tags that both users have. The same logic suits shopping-basket overlap, near-duplicate detection with MinHashing, and Intersection over Union (IoU) for bounding boxes.

How they relate on 0/1 data

For binary vectors, cosine similarity can be written with sets:

\[ \cos\theta = \frac{|A \cap B|}{\sqrt{|A|\,|B|}} \]

The union is at least as large as each set, so it is at least \(\sqrt{|A|\,|B|}\). Hence, for non-empty sets:

\[ \cos\theta \ge J(A, B) \]

The two are equal only when the sets are identical (both 1) or share nothing (both 0).

Choosing quickly

TipRule of thumb
  • Use Jaccard when the data is naturally a set, and we care about the fraction of items shared.
  • Use cosine when the features have counts or weights, and we care about their proportions rather than their size.

References

R-bloggers

R posts from Stat Mania are also shared on R-bloggers, a hub of R blogs.