TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
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=2⋅TP2⋅TP+FP+FNF_{1_{\text{micro}}} = \frac{2 \cdot TP}{2 \cdot TP + FP + FN} F1micro​​=2⋅TP+FP+FN2⋅TP​

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=2⋅22⋅2+1+1=46=23≈0.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 TP=2→F1micro​​=2⋅2+1+12⋅2​=64​=32​≈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
Classification MetricsAucIou Bounding BoxConfusion Matrix NormLog Loss Per Sample

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.
PrevNext

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=2⋅TP2⋅TP+FP+FNF_{1_{\text{micro}}} = \frac{2 \cdot TP}{2 \cdot TP + FP + FN} F1micro​​=2⋅TP+FP+FN2⋅TP​

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=2⋅22⋅2+1+1=46=23≈0.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 TP=2→F1micro​​=2⋅2+1+12⋅2​=64​=32​≈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
Classification MetricsAucIou Bounding BoxConfusion Matrix NormLog Loss Per Sample

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.