Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Custom Python Evaluation Metric

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

Your question is Custom Python Evaluation Metric. 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

NTT DATA evaluates binary classification outputs from AI solutions. Implement a custom F1-score metric from scratch using only basic Python arrays and arithmetic, without importing machine learning libraries.

Given two arrays, y_true and y_pred, containing binary labels, calculate the F1-score:

F1 = 2 * TP / (2 * TP + FP + FN)

Count true positives, false positives, and false negatives during one pass through the arrays. True negatives do not affect the F1-score. If the denominator is zero, return 0.0.

Formal Specification

  • Input: Two arrays of integers, y_true and y_pred, with equal length. Each value is either 0 or 1.
  • Output: A floating-point F1-score between 0.0 and 1.0.
  • Do not round the result inside the function.

Constraints

  • 1 <= len(y_true) <= 10^6
  • len(y_true) == len(y_pred)
  • Each value in both arrays is either 0 or 1
  • Return 0.0 when TP + FP + FN is zero

Function Signature

def binary_f1_score(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