they're already sorted. [1, 0, 1, 1, 1, 0, 0, 0]
- User
they're already sorted. [1, 0, 1, 1, 1, 0, 0, 0]
Assistant
If your predictions are already sorted in descending order of confidence (i.e., the most confident predictions come first), and you're given a binary label list like:
[1, 0, 1, 1, 1, 0, 0, 0]Then we can simplify the Average Precision (AP) computation. Since everything is already ranked, we don't need to sort by scores anymore.
Simplified AP Formula:
For each position , if the label at is
1, compute precision at , then average over all such positions.
✅ Lambda Function Using NumPy
Here's a clean and efficient lambda function using NumPy:
import numpy as np average_precision = lambda y_true: ( np.sum((np.cumsum(y_true) / np.arange(1, len(y_true)+1)) * y_true) / np.sum(y_true) ) if np.sum(y_true) > 0 else 0
📌 Example:
y_true = np.array([1, 0, 1, 1, 1, 0, 0, 0]) ap_score = average_precision(y_true) print(f"Average Precision: {ap_score:.4f}")🔍 Step-by-step for this example:
Rank Label CumSum Precision 1 1 1 1/1 = 1.0 2 0 1 - 3 1 2 2/3 ≈ 0.67 4 1 3 3/4 = 0.75 5 1 4 4/5 = 0.80 6 0 4 - 7 0 4 - 8 0 4 - Only true positives contribute: ranks 1, 3, 4, 5
✅ Output:
Average Precision: 0.8050Let me know if you want to extend this to multiple queries to compute full MAP!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.