Problems
Loading...
1 / 1

Compute Accuracy, Precision, Recall, F1

Metrics & Evaluation
Medium

Compute accuracy, precision, recall, and F1 for single-label predictions. Support averaging modes:

  • micro (default): global TP/FP/FN aggregation
  • macro: unweighted mean of per-class metrics
  • weighted: class-weighted mean by support
  • binary: metrics for a specified positive class pos_label
Loading visualization...

Examples

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

Output (micro): accuracy≈0.75, precision≈0.75, recall≈0.75, f1≈0.75

Hint 1

Build a confusion matrix first, then compute TP, FP, FN for each class from the matrix.

Hint 2

For micro averaging, sum all TP, FP, FN across classes. For macro, compute per-class metrics then average.

Requirements

  • Inputs: equal-length arrays of integer labels
  • Single-label multi-class classification
  • No external ML libraries
  • Return dict: {"accuracy": float, "precision": float, "recall": float, "f1": float}

Constraints

  • n ≤ 10⁵
  • Labels in 0..K-1 (or arbitrary ints)
Try Similar Problems