Loading content...
In the realm of machine learning model evaluation, precision stands as one of the most critical metrics for understanding how reliable your positive predictions are. When a model predicts that something belongs to the positive class, precision tells us the probability that this prediction is actually correct.
The Precision Formula: Precision is mathematically defined as the ratio of correctly identified positive cases (True Positives) to all cases predicted as positive (True Positives + False Positives):
$$\text{Precision} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Positives}}$$
Understanding the Components:
When Precision Matters Most: Precision is particularly valuable in scenarios where false positives are costly. For instance, in spam detection, a false positive means a legitimate email is marked as spam—potentially causing users to miss important messages. In fraud detection, false positives can lead to unnecessary account freezes, frustrating honest customers.
Your Task: Write a Python function that computes the precision score given two numpy arrays: the ground truth labels and the predicted labels. Both arrays contain binary values (0 or 1), where 1 represents the positive class.
Edge Case Handling: If there are no positive predictions (i.e., TP + FP = 0), the precision is undefined mathematically. In such cases, return 0.0 to handle the division by zero scenario gracefully.
y_true = [1, 0, 1, 1, 0, 1]
y_pred = [1, 0, 1, 0, 0, 1]1.0Let's analyze each prediction:
• Index 0: Predicted 1, Actual 1 → True Positive • Index 1: Predicted 0, Actual 0 → True Negative (not counted in precision) • Index 2: Predicted 1, Actual 1 → True Positive • Index 3: Predicted 0, Actual 1 → False Negative (not counted in precision) • Index 4: Predicted 0, Actual 0 → True Negative (not counted in precision) • Index 5: Predicted 1, Actual 1 → True Positive
True Positives (TP) = 3 False Positives (FP) = 0
Precision = TP / (TP + FP) = 3 / (3 + 0) = 3 / 3 = 1.0
Perfect precision! Every positive prediction made by the model was correct.
y_true = [1, 0, 1, 0, 0, 0]
y_pred = [1, 1, 1, 1, 0, 0]0.5Analyzing the predictions:
• Index 0: Predicted 1, Actual 1 → True Positive • Index 1: Predicted 1, Actual 0 → False Positive • Index 2: Predicted 1, Actual 1 → True Positive • Index 3: Predicted 1, Actual 0 → False Positive • Index 4: Predicted 0, Actual 0 → True Negative • Index 5: Predicted 0, Actual 0 → True Negative
True Positives (TP) = 2 False Positives (FP) = 2
Precision = TP / (TP + FP) = 2 / (2 + 2) = 2 / 4 = 0.5
Half of the positive predictions were correct, half were incorrect.
y_true = [1, 1, 1, 0, 0, 0]
y_pred = [1, 1, 1, 0, 0, 0]1.0This represents a perfect classifier:
• Indices 0-2: All predicted as 1, all actually 1 → 3 True Positives • Indices 3-5: All predicted as 0, all actually 0 → 3 True Negatives
True Positives (TP) = 3 False Positives (FP) = 0
Precision = TP / (TP + FP) = 3 / (3 + 0) = 1.0
The model achieved perfect precision with no false alarms.
Constraints