Metrics ยท Easy
precision_at_k
Implement a ranking metric that measures how many relevant examples appear in the top k results after sorting by model score.
Task
Write precision_at_k(labels, scores, k). The input
labels are binary values where 1 means relevant and
0 means not relevant. Sort examples by descending
score and return the mean label among the top k examples.
Function Signature
def precision_at_k(labels: list[int], scores: list[float], k: int) -> float:
pass
Rules
- If
k <= 0, return0.0. - If
kis larger than the number of examples, use all examples. - If two scores tie, keep the original input order.
- Assume
labelsandscoreshave the same length.
Examples
labels = [0, 1, 1, 0]
scores = [0.2, 0.9, 0.4, 0.7]
precision_at_k(labels, scores, 2)
# 0.5
precision_at_k(labels, scores, 3)
# 0.6666666667