Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Precision and Recall for Multi-Class

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

Your question is Precision and Recall for Multi-Class. 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

The RealReal evaluates a classifier that assigns categories to fashion listings. Given the true category and predicted category for each listing, calculate precision and recall independently for every requested class.

Formal Specification

Implement multiclass_precision_recall(y_true, y_pred, labels), where y_true, y_pred, and labels are lists of strings. y_true[i] is the correct class for item i, and y_pred[i] is the model's prediction. The lists y_true and y_pred have equal length. Return a dictionary mapping each label to a dictionary containing numeric precision and recall values.

For class c:

  • precision = TP / (TP + FP), where the denominator is the number of predictions equal to c.
  • recall = TP / (TP + FN), where the denominator is the number of true labels equal to c.
  • If either denominator is zero, return 0.0 for that metric.

Preserve the labels supplied by the caller, including classes that do not appear in either input. Do not calculate macro or micro averages.

Constraints

  • 1 <= len(y_true) = len(y_pred) <= 10^5
  • 1 <= len(labels) <= 10^4
  • Labels are unique, non-empty strings
  • Every value in y_true and y_pred appears in labels

Function Signature

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