Jaccard Similarity, also known as the Jaccard Index or Jaccard Coefficient, measures the similarity between two sets. Commonly used in:
- Text and document similarity
- Recommendation systems
- Duplicate and plagiarism detection
- Clustering and classification
- Binary data and image comparison
It is calculated as the size of the intersection divided by the size of the union of the two sets.
Formula
J(A, B) = \frac{|A \cap B|}{|A \cup B|}
where:
- ∣A∩B∣ is the number of common elements between sets
- ∣A∪B∣ is the total number of unique elements in both sets.
Value of Jaccard similarity ranges from 0 to 1:
- 1 means both sets are identical.
- 0 means the sets have no elements in common.
- Value between 0 and 1 represents partial similarity.

Jaccard Similarity Between Two Sets
Python sets provide built-in operations for finding intersections and unions, which makes them suitable for calculating Jaccard Similarity. The & operator gives the intersection, while the | operator gives the union.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
intersection = A & B
union = A | B
jaccard_similarity = len(intersection) / len(union)
print("Jaccard Similarity:", jaccard_similarity)
Output
Jaccard Similarity: 0.3333333333333333
Explanation:
- A & B finds the elements present in both sets.
- A | B combines all unique elements from both sets.
- len(intersection) gives the number of common elements.
- len(union) gives the number of unique elements across both sets.
- Dividing these values gives the Jaccard Similarity.
Jaccard Similarity Between Binary Vectors
Jaccard Similarity can also be calculated for binary vectors containing only 0 and 1. For binary vectors, only positions containing at least one 1 contribute to the calculation.
Formula:
J(A, B) = \frac{M_{11}}{M_{01} + M_{10} + M_{11}}
where:
- M11 is the number of positions where both vectors contain 1.
- M10 is the number of positions where A contains 1 and B contains 0.
- M01 is the number of positions where A contains 0 and B contains 1.
Numerical Example
Consider the binary vectors:
A = [1, 1, 0, 1, 0, 1, 0]
B = [1, 0, 0, 1, 1, 1, 0]
Step by step calculations:
- M11 = 3 (positions: 1st, 4th, 6th)
- M10 = 1 (position: 2nd)
- M01 = 1 (position: 5th)
Python Implementation with Visualization
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import jaccard_score
A = np.array([1, 1, 0, 1, 0, 1, 0])
B = np.array([1, 0, 0, 1, 1, 1, 0])
similarity = jaccard_score(A, B)
print(f"Jaccard Similarity: {similarity:.2f}")
Output
Jaccard Similarity: 0.60
Calculate Using Scikit-learn
Scikit-learn library provides the jaccard_score() function for calculating jaccard similarity.
from sklearn.metrics import jaccard_score
y_true = [1, 1, 0, 1, 0, 1, 0]
y_pred = [1, 0, 0, 1, 1, 1, 0]
score = jaccard_score(y_true, y_pred)
print("Jaccard Similarity:", score)
Output
Jaccard Similarity: 0.6
Explanation:
- y_true contains the actual binary values.
- y_pred contains the corresponding predicted values.
- jaccard_score() compares both sequences.
- Resulting value represents their Jaccard Similarity.
Jaccard Similarity for Strings
It can also be used to compare strings by treating their words as sets.
A = set("Python is easy to learn".split())
B = set("Python is easy and powerful".split())
similarity = len(A & B) / len(A | B)
print("Jaccard Similarity:", similarity)
Output
Jaccard Similarity: 0.42857142857142855
Handle Empty Sets
When both sets are empty, the Jaccard Similarity formula results in division by zero because their union has no elements.
def jaccard_similarity(A, B):
union = A | B
if not union:
return 1.0
return len(A & B) / len(union)
A = set()
B = set()
print(jaccard_similarity(A, B))
Output
1.0
Explanation:
- Both of the sets are empty, so they contain no differences.
- Function returns 1.0 to represent identical sets.