← back

Calculate Performance Metrics for a Classification Model

#77 · Machine Learning · Medium

⊣ Solve on deep-ml.com

Problem

Calculate multiple performance metrics for a binary classification model: accuracy, precision, recall, and F1 score, given true labels and predicted labels.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def classification_metrics(y_true, y_pred):
    tp = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 1)
    tn = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 0)
    fp = sum(1 for t, p in zip(y_true, y_pred) if t == 0 and p == 1)
    fn = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 0)

    n = len(y_true)
    accuracy = (tp + tn) / n if n > 0 else 0
    precision = tp / (tp + fp) if (tp + fp) > 0 else 0
    recall = tp / (tp + fn) if (tp + fn) > 0 else 0
    f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0

    return {
        "accuracy": round(accuracy, 4),
        "precision": round(precision, 4),
        "recall": round(recall, 4),
        "f1_score": round(f1, 4)
    }

Explanation

  1. Count TP, TN, FP, FN from the true and predicted label pairs.
  2. Accuracy = (TP + TN) / total: overall correct predictions.
  3. Precision = TP / (TP + FP): of all predicted positives, how many are correct.
  4. Recall = TP / (TP + FN): of all actual positives, how many were found.
  5. F1 Score = harmonic mean of precision and recall, balancing both metrics.

Complexity

  • Time: O(n) where n is the number of samples
  • Space: O(1)