Problems
Loading...
1 / 1

Implement Micro-F1

Metrics & Evaluation
Easy

Micro-averaging aggregates the total true positives, false positives, and false negatives across all classes before computing F1:

F1micro=2TP2TP+FP+FNF_{1_{\text{micro}}} = \frac{2 \cdot TP}{2 \cdot TP + FP + FN}

y_true and y_pred are equal-length sequences of integer labels (single-label, multi-class).

Return a float in [0, 1].

Loading visualization...

Examples

y_true=[0,1,1], y_pred=[0,1,0]

TP=2    F1micro=2222+1+1=46=230.6667TP = 2 \; \rightarrow \; F_{1_{\text{micro}}} = \frac{2 \cdot 2}{2 \cdot 2 + 1 + 1} = \frac{4}{6} = \frac{2}{3} \approx 0.6667

y_true=[0,1,2,2], y_pred=[0,1,2,2]

Output: 1.0

y_true=[2,2,1,0], y_pred=[1,2,1,0]

TP=3 out of 4 → 0.75

Hint 1

Convert inputs to NumPy arrays first.

Hint 2

Calculate TP by counting element-wise matches between arrays, then derive FP and FN from the total length.

Requirements

  • Handle up to 10⁵ items
  • Integer labels assumed to be in 0..K-1 (K inferred from data)
  • Return a Python float (not NumPy scalar)

Constraints

  • len(y_true) == len(y_pred)
  • No external ML libraries allowed.
Try Similar Problems