Multiclass precision and recall
Your task
Given y_true and y_pred (equal-length lists of integer class labels in 0..k-1) and the number of classes k, compute the per-class precision and recall from the multiclass confusion matrix. For class c, precision = TP_c / (TP_c + FP_c) and recall = TP_c / (TP_c + FN_c), where TP_c is the count of samples with true=c and pred=c, FP_c is true!=c but pred=c, and FN_c is true=c but pred!=c. A zero denominator yields 0.0 for that metric. Return a list of k pairs [precision, recall] for classes 0..k-1, each value rounded to 4 decimals.
Examples
per_class_metrics(y_true: list[int], y_pred: list[int], k: int) -> list[list[float]]Example 1
Input
per_class_metrics([0, 1, 2, 0, 1], [0, 1, 1, 0, 1], 3)Output
[[1, 1], [0.6667, 1], [0, 0]]