Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

195. Precision recall curve

medium
GeneralGeneral
senior

Build a Precision–Recall (PR) curve for a binary classifier so you can visualize the trade-off between precision and recall across different decision thresholds. You’ll compute precision/recall points by sweeping thresholds over predicted probabilities and return the curve data for evaluation.

Requirements

Implement the function

python

Rules:

  • Use thresholds equal to the unique values in y_score, sorted in descending order.
  • For each threshold (t), predict (\hat{y}=1) if (y_score \ge t) else (0), then compute: precision=TPTP+FP,recall=TPTP+FN\text{precision}=\frac{TP}{TP+FP}, \quad \text{recall}=\frac{TP}{TP+FN}
  • Return three NumPy arrays: precisions, recalls, and thresholds.
  • Do not use any prebuilt PR-curve utilities (e.g., from scikit-learn).
  • Use NumPy vectorized operations for efficiency.

Example

python

Output:

python
Input Signature
ArgumentType
y_truenp.ndarray
y_scorenp.ndarray
Output Signature
Return NameType
valuetuple

Constraints

  • No sklearn PR-curve utilities

  • Return three NumPy arrays

  • Use vectorized operations (O(N log N) time)

Hint 1

Use argsort on -y_score to sort scores in descending order.

Hint 2

Use cumsum on sorted labels to compute TP counts efficiently; FP = k - TP for top-k predictions.

Hint 3

Identify indices where y_score changes to extract only unique threshold points.

Roles
ML Engineer
AI Engineer
Data Scientist
Quantitative Analyst
Companies
GeneralGeneral
Levels
senior
entry
Tags
precision-recall
threshold sweep
binary classification
metrics
26 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit