Jaccard similarityEasynumpymetricssimilaritysetsiou
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...
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: 2/3
- •Identical vectors -> 1.0
- •Disjoint vectors -> 0.0
- •Empty union -> 0.0 (no division by zero)