Precision Score
~12 mincode completion
Implement . Assume at least one positive prediction exists.
Examples
- Input
- precision([1, 0, 1, 0, 1], [1, 1, 1, 0, 0])
- Output
- 0.66667
- Input
- precision([1, 1, 1, 0], [1, 1, 0, 0])
- Output
- 1
- Input
- precision([0, 0, 1, 1], [1, 1, 1, 0])
- Output
- 0.33333
Hints
Hint 1
Sum with , and check which axis you are summing over.
Hint 2
Watch for this: confused fp and fn.
Requirements
y_true: Ground truth binary labels (0 or 1)y_pred: Predicted binary labels (0 or 1)Return Precision score in [0, 1].
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~12 min
••••••••••••••••
8 employers weight this skill
3 AI product companies, 2 frontier labs, 1 health and bio company, 1 big tech firm, 1 autonomy company. Top match scores 93.
Python
import numpy as np
def precision(y_true: np.ndarray, y_pred: np.ndarray) -> float:
"""
Compute precision = TP / (TP + FP).
Args:
y_true: Ground truth binary labels (0 or 1)
y_pred: Predicted binary labels (0 or 1)
Returns:
Precision score in [0, 1].
"""
# YOUR CODE HERE
pass