#273Jaccard similarityEasyComputer VisionEvaluation MetricsEmbeddings
Jaccard similarity
Background
The Jaccard similarity of two sets is the size of their intersection over the size of their union — a number in measuring overlap. On binary vectors (presence/absence indicators) it is the standard similarity for sparse data: document shingles, market baskets, and segmentation masks, where it goes by the name Intersection-over-Union.
Problem statement
Implement jaccard_similarity(a, b) for two equal-length binary vectors:
Input
a— array-like of 0/1 values.b— array-like of 0/1 values, the same length.
Output
Returns a float in .
Examples
Example 1
Input: a = [1, 1, 0, 1], b = [1, 0, 0, 1]
Output: 0.6667
Explanation: both are 1 at positions 0 and 3 (intersection = 2); at least one is 1 at positions 0, 1, 3 (union = 3), so .
Constraints
- Intersection = count of positions where both are 1; union = count where at least one is 1.
- If the union is empty (both vectors all-zero), return to avoid .
- Values are binary.
Notes
- On segmentation masks this is exactly IoU (Intersection-over-Union), the standard overlap metric in detection and segmentation.
- Jaccard counts only co-presence (1-1) and ignores 0-0 agreement, which makes it well-suited to sparse data where most entries are 0.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: two of three positions overlap
- •Reference: identical vectors give 1.0
- •Sample: half of the union overlaps