Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Classification Metrics Evaluation

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

Your question is Classification Metrics Evaluation. 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

In InstaDeep's DeepChain model-evaluation workflow, implement a function that computes standard binary classification metrics from predicted labels and ground-truth labels. Return accuracy, precision, recall, and F1 score without using external machine-learning libraries.

Formal Specification

Given two lists, predictions and labels, containing binary values, and an integer positive_label, return a dictionary with the floating-point keys accuracy, precision, recall, and f1.

Use these definitions:

  • accuracy = (TP + TN) / N
  • precision = TP / (TP + FP)
  • recall = TP / (TP + FN)
  • f1 = 2 * precision * recall / (precision + recall)

If a metric has a zero denominator, return 0.0 for that metric. In particular, F1 is 0.0 when both precision and recall are zero. The input lists are guaranteed to have equal, nonzero lengths.

Constraints

  • 1 <= len(predictions) = len(labels) <= 10^6
  • Each prediction and label is either 0 or 1
  • positive_label is either 0 or 1
  • The input lists are nonempty and have equal length
  • A metric with a zero denominator must be returned as 0.0

Function Signature

def compute_classification_metrics(predictions, labels, positive_label):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output