Evaluation Metrics

easy · evaluation, accuracy, precision, recall

Evaluation Metrics

Implement classification evaluation metrics from scratch.

Functions to implement

1. accuracy(y_true, y_pred)

Compute accuracy (fraction correct).

  • Input: Lists of true and predicted labels
  • Output: Fraction of correct predictions

2. confusion_matrix(y_true, y_pred)

Compute the 2x2 confusion matrix for binary classification.

  • Input: Lists of true (0/1) and predicted (0/1) labels
  • Output: Dictionary with keys 'TP', 'TN', 'FP', 'FN'

3. precision(y_true, y_pred)

Compute precision = TP / (TP + FP).

  • Input: Lists of true and predicted labels
  • Output: Precision score

4. recall(y_true, y_pred)

Compute recall = TP / (TP + FN).

  • Input: Lists of true and predicted labels
  • Output: Recall score

5. f1_score(y_true, y_pred)

Compute F1 = 2 (precision recall) / (precision + recall).

  • Input: Lists of true and predicted labels
  • Output: F1 score

Examples

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

accuracy(y_true, y_pred)         # 0.8
confusion_matrix(y_true, y_pred) # {'TP': 2, 'TN': 2, 'FP': 0, 'FN': 1}
precision(y_true, y_pred)        # 1.0 (no false positives)
recall(y_true, y_pred)           # 0.667 (missed one positive)
f1_score(y_true, y_pred)         # 0.8

Notes

  • Assume binary classification (labels are 0 or 1)
  • Handle edge cases (e.g., no predictions of class 1)
Run tests to see results
No issues detected