Dice Score for Classification
Core Vision Operations DS practice problem on Onlearn.
Difficulty: medium.
Topics: Dice Score for Classification, Dice Coefficient, Jaccard Index, True Positive Rate, Soft Dice Loss, Intersection over Union, Computer Vision, Information Theory, Statistical Learning, Optimization Theory, Performance Evaluation, Semantic Segmentation, Overlap Metrics, Loss Function Design, Binary Classification, Differentiable Programming.
Task: Compute the Dice Score Your task is to implement a function dice score(y true, y pred) that calculates the Dice Score, also known as the Sørensen Dice coefficient or F1 score, for binary classification. The Dice Score is used to measure the similarity between two sets and is particularly useful in tasks like image segmentation and binary classification. Your Task: Implement the function dice score(y true, y pred) to: 1. Calculate the Dice Score between the arrays y true and y pred. 2. Return the Dice Score as a float value rounded to 3 decimal places. 3. Handle edge cases appropriately, such as when there are no true or predicted positives. The Dice Score is defined as: $$ \scriptsize \text{Dice Score} = \frac{2 \times (\text{Number of elements in the intersection of } y {\text{true}} \text{ and } y {\text{pred}})}{\text{Number of elements in } y {\text{true}} + \text{Number of elements in } 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).