Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Precision and Recall Function

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Precision and Recall Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

For a binary classification model used in a 7-Eleven customer offer pipeline, calculate precision and recall from true labels and predicted probability scores. A score greater than or equal to the supplied threshold is classified as positive.

Implement precision_recall_at_threshold without using machine learning libraries.

Formal Specification

The function receives:

  • y_true: a list of integers containing only 0 and 1, where 1 is the positive class.
  • scores: a list of numeric model scores, with one score for each label.
  • threshold: a numeric cutoff.

Return a dictionary with numeric keys precision and recall, each represented as a floating-point value. If precision or recall has a zero denominator, return 0.0 for that metric.

Constraints

  • 1 <= len(y_true) = len(scores) <= 10^5
  • Every value in y_true is either 0 or 1
  • Scores and threshold are numeric
  • 0 <= threshold <= 1

Function Signature

def precision_recall_at_threshold(y_true, scores, threshold):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output