← All Exercises

Compute Precision, Recall and F1

basicjavascript~20min

Background

Model quality claims collapse when the metric is undefined for a class. Defining the zero-division behaviour makes offline evaluation reproducible.

Problem

From ground-truth and predicted labels, compute precision, recall and F1, returning zero instead of NaN when the denominator is empty.

Input

JSON object: { yTrue: number[], yPred: number[] }

Output

JSON object: { precision, recall, f1 } rounded to four decimals

Sample tests

Sample 1

Input: {"yTrue":[1,1,0,0,1],"yPred":[1,0,1,0,1]}
Expected: {"precision":0.6667,"recall":0.6667,"f1":0.6667}

Sample 2

Input: {"yTrue":[0,0],"yPred":[0,0]}
Expected: {"precision":0,"recall":0,"f1":0}

Your solution

Learning objective

Report classification quality with explicit zero-division rules.