Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Precision and Recall on Imbalanced Data

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

Your question is Precision and Recall on Imbalanced Data. 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

Vertafore may evaluate classification models used with products such as AMS360, where positive cases can be much less common than negative cases. Given actual and predicted binary labels, calculate precision and recall without using machine learning libraries.

Formal Specification

Implement precision_recall(y_true, y_pred), where both inputs are non-empty lists of integers containing only 0 and 1, and have equal length. Label 1 is the positive class. Return a dictionary with two floating-point values:

  • precision = TP / (TP + FP), or 0.0 when the model predicts no positive cases.
  • recall = TP / (TP + FN), or 0.0 when the input contains no actual positive cases.

Count each label pair once. Do not round the results.

Constraints

  • 1 <= len(y_true) = len(y_pred) <= 10^6
  • Every value in both lists is either 0 or 1
  • The positive class is represented by 1
  • The input lists have equal length
  • Return 0.0 for either metric when its denominator is zero

Function Signature

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