Jaccard Index for Binary Classification

Core Vision Operations DS practice problem on Onlearn.

Difficulty: easy.

Topics: Understanding Calculate Jaccard Index for Binary Classification, Intersection over Union, Binary Masking, Floating Point Precision, Edge Case Validation, Vectorized Computation, Computer Vision, Information Theory, Statistical Learning, Numerical Computing, Software Engineering, Image Segmentation, Performance Metrics, Set Theory Operations, Array Manipulation, Error Handling.

Task: Implement the Jaccard Index Your task is to implement a function jaccard index(y true, y pred) that calculates the Jaccard Index, a measure of similarity between two binary sets. The Jaccard Index is widely used in binary classification tasks to evaluate the overlap between predicted and true labels. Your Task: Implement the function jaccard index(y true, y pred) to: 1. Calculate the Jaccard Index between the arrays y true and y pred. 2. Return the Jaccard Index as a float value. 3. Ensure the function handles cases where: There is no overlap between y true and y pred. Both arrays contain only zeros (edge cases). The Jaccard Index is defined as: $$ \scriptsize \text{Jaccard Index} = \frac{\text{Number of elements in the intersection of } y {\text{true}} \text{ and } y {\text{pred}}}{\text{Number of elements in the union of } y {\text{true}} \text{ and } y {\text{pred}}} $$ Where: $ y {\text{true}} $ and $ y {\text{pred}} $ are binary arrays of the same length, representing true and predicted labels. The result ranges from 0 (no overlap) to 1 (perfect overlap).