Click4Ai

68.

Easy

Implement the **Recall (Sensitivity)** score for binary classification.

Formula:

Recall = TP / (TP + FN)

Where:

  • **TP (True Positives):** Predicted 1 AND actually 1
  • **FN (False Negatives):** Predicted 0 BUT actually 1
  • Write a function recall(y_true, y_pred) that returns the recall score.

    Example:

    y_true = [1, 0, 1, 1, 0, 1, 0, 1]

    y_pred = [1, 0, 0, 1, 1, 1, 0, 1]

    recall(y_true, y_pred) → 0.8

    **Explanation:** There are 5 actual positives (indices 0,2,3,5,7). The model correctly predicted 4 of them (TP=4) and missed 1 (FN=1 at index 2). Recall = 4/5 = 0.8.

    Constraints:

  • Return 0.0 if there are no actual positives (TP + FN = 0)
  • Return a float between 0.0 and 1.0
  • Test Cases

    Test Case 1
    Input: y_true=[1,0,1,1,0,1,0,1], y_pred=[1,0,0,1,1,1,0,1]
    Expected: 0.8
    Test Case 2
    Input: y_true=[1,1,1], y_pred=[1,1,1]
    Expected: 1.0
    Test Case 3
    Input: y_true=[1,1,1], y_pred=[0,0,0]
    Expected: 0.0
    + 2 hidden test cases