TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Compute Accuracy, Precision, Recall, F1

Metrics & Evaluation
Medium

Compute accuracy, precision, recall, and F1 for single-label classification. For each class ccc, let TPcTP_cTPc​ count correct predictions of ccc, FPcFP_cFPc​ count predictions of ccc whose true label differs, and FNcFN_cFNc​ count occurrences of ccc predicted as another class.

Pc=TPcTPc+FPcP_c = \frac{TP_c}{TP_c + FP_c}Pc​=TPc​+FPc​TPc​​ Rc=TPcTPc+FNcR_c = \frac{TP_c}{TP_c + FN_c}Rc​=TPc​+FNc​TPc​​ F1,c=2PcRcPc+RcF_{1,c} = \frac{2P_cR_c}{P_c + R_c}F1,c​=Pc​+Rc​2Pc​Rc​​

Use micro to aggregate class counts before computing metrics, macro to average class metrics equally, weighted to average them by true-label support, and binary to report the class selected by pos_label. A zero denominator contributes 0.0. Return accuracy, precision, recall, and f1 in a dictionary, with every value rounded to six decimals.

Loading visualization...

Examples

Input: y_true = [0, 1, 2, 2], y_pred = [0, 1, 0, 2], average = "micro", pos_label = 1

Output: {"accuracy": 0.75, "precision": 0.75, "recall": 0.75, "f1": 0.75}

Explanation: Three of four labels are correct; for single-label micro averaging, precision, recall, and F1 also equal 0.75.

Input: y_true = [0, 1, 2, 2], y_pred = [0, 1, 0, 2], average = "macro", pos_label = 1

Output: {"accuracy": 0.75, "precision": 0.833333, "recall": 0.833333, "f1": 0.777778}

Hint 1

Use np.unique(np.concatenate([y_true, y_pred])) to collect every observed class.

Hint 2

For each class, boolean masks can count true positives, false positives, and false negatives.

Hint 3

Use the true-label count of each class as its weight for weighted averaging.

Requirements

  • Compute accuracy across all samples
  • Support micro, macro, weighted, and binary averaging
  • Use 0.0 when a precision, recall, or F1 denominator is zero
  • Return exactly the keys accuracy, precision, recall, and f1
  • Round every returned value to six decimals

Constraints

  • y_true and y_pred are equal-length, nonempty lists of integer labels
  • average is micro, macro, weighted, or binary
  • At most 100,000100{,}000100,000 labels are provided
  • Do not use an external machine learning library
Try Similar Problems
Metrics F1 MicroAucLog Loss Per SampleConfusion Matrix NormCohens Kappa

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: string

Accepts: number

You must run your code first.
PrevNext

Compute Accuracy, Precision, Recall, F1

Metrics & Evaluation
Medium

Compute accuracy, precision, recall, and F1 for single-label classification. For each class ccc, let TPcTP_cTPc​ count correct predictions of ccc, FPcFP_cFPc​ count predictions of ccc whose true label differs, and FNcFN_cFNc​ count occurrences of ccc predicted as another class.

Pc=TPcTPc+FPcP_c = \frac{TP_c}{TP_c + FP_c}Pc​=TPc​+FPc​TPc​​ Rc=TPcTPc+FNcR_c = \frac{TP_c}{TP_c + FN_c}Rc​=TPc​+FNc​TPc​​ F1,c=2PcRcPc+RcF_{1,c} = \frac{2P_cR_c}{P_c + R_c}F1,c​=Pc​+Rc​2Pc​Rc​​

Use micro to aggregate class counts before computing metrics, macro to average class metrics equally, weighted to average them by true-label support, and binary to report the class selected by pos_label. A zero denominator contributes 0.0. Return accuracy, precision, recall, and f1 in a dictionary, with every value rounded to six decimals.

Loading visualization...

Examples

Input: y_true = [0, 1, 2, 2], y_pred = [0, 1, 0, 2], average = "micro", pos_label = 1

Output: {"accuracy": 0.75, "precision": 0.75, "recall": 0.75, "f1": 0.75}

Explanation: Three of four labels are correct; for single-label micro averaging, precision, recall, and F1 also equal 0.75.

Input: y_true = [0, 1, 2, 2], y_pred = [0, 1, 0, 2], average = "macro", pos_label = 1

Output: {"accuracy": 0.75, "precision": 0.833333, "recall": 0.833333, "f1": 0.777778}

Hint 1

Use np.unique(np.concatenate([y_true, y_pred])) to collect every observed class.

Hint 2

For each class, boolean masks can count true positives, false positives, and false negatives.

Hint 3

Use the true-label count of each class as its weight for weighted averaging.

Requirements

  • Compute accuracy across all samples
  • Support micro, macro, weighted, and binary averaging
  • Use 0.0 when a precision, recall, or F1 denominator is zero
  • Return exactly the keys accuracy, precision, recall, and f1
  • Round every returned value to six decimals

Constraints

  • y_true and y_pred are equal-length, nonempty lists of integer labels
  • average is micro, macro, weighted, or binary
  • At most 100,000100{,}000100,000 labels are provided
  • Do not use an external machine learning library
Try Similar Problems
Metrics F1 MicroAucLog Loss Per SampleConfusion Matrix NormCohens Kappa

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: string

Accepts: number

You must run your code first.