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:
- Calculate the Jaccard Index between the arrays
y_trueandy_pred. - Return the Jaccard Index as a float value.
- Ensure the function handles cases where:
- There is no overlap between
y_trueandy_pred. - Both arrays contain only zeros (edge cases).
- There is no overlap between
The Jaccard Index is defined as:
Jaccard Index=Number of elements in the union of ytrue and ypredNumber of elements in the intersection of ytrue and ypredWhere:
- ytrue and ypred are binary arrays of the same length, representing true and predicted labels.
- The result ranges from 0 (no overlap) to 1 (perfect overlap).
Examples
Example 1:
Input:
y_true = np.array([1, 0, 1, 1, 0, 1])
y_pred = np.array([1, 0, 1, 0, 0, 1])
print(jaccard_index(y_true, y_pred))Output:
0.75Explanation: The Jaccard Index is calculated as 3 / 4 = 0.75, indicating a 75% overlap between the true and predicted labels.
Starter Code
import numpy as np
def jaccard_index(y_true, y_pred):
# Write your code here
return round(result, 3)
Python3
ReadyLines: 1Characters: 0
Ready