Jaccard similarityEasy

Jaccard similarity

Background

The Jaccard similarity of two sets is the size of their intersection over the size of their union — a number in [0,1][0, 1] 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:

J(a,b)=abab=i(aibi)i(aibi)J(a, b) = \frac{|a \cap b|}{|a \cup b|} = \frac{\sum_i (a_i \wedge b_i)}{\sum_i (a_i \vee b_i)}

Input

  • a — array-like of 0/1 values.
  • b — array-like of 0/1 values, the same length.

Output

Returns a float in [0,1][0, 1].

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 J=2/3=0.6667J = 2/3 = 0.6667.

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 0.00.0 to avoid 0/00/0.
  • 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)