Evaluation Metrics
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
Input domain and boundary policy
Inputs are equal-length, nonempty lists of binary labels (0 or 1), with 1 as
the positive class. confusion_matrix returns the four-key dictionary, not a
nested list. Precision returns 0.0 if TP+FP is zero; recall returns 0.0 if
TP+FN is zero; F1 returns 0.0 if precision+recall is zero. These are explicit
exercise conventions for undefined ratios. Do not mutate inputs. Threshold
selection belongs outside these functions: they receive final labels.
Run tests to see results
No issues detected