Premium problem131. Confusion Matrix and Macro F1

Medium Locked

Given integer arrays y_true and y_pred with labels in [0, C), return (cm, macro_f1):

  • cm is the (C, C) matrix where cm[i, j] counts samples of true class i predicted as j
  • macro_f1 is the unweighted mean of per-class F1
  • substitute 0 for any class whose precision + recall is 0

Input

y_true = [0 1 2 1 0]
y_pred = [0 2 2 1 0]
C = 3

Output

(array([[2, 0, 0],
       [0, 1, 1],
       [0, 0, 1]]), 0.7777777777777777)

Premium problem

This one's part of Premium. Unlock the full NumPy track plus every other premium problem on the site.

Implement solve(...)