Evaluation Metrics
🎯 Learning Objectives
- Construct and interpret a confusion matrix
- Calculate Precision, Recall, F1-Score, Specificity
- Understand ROC curves and AUC
- Apply regression metrics: MSE, RMSE, MAE, R²
- Handle multi-class averaging (macro, micro, weighted)
1. Confusion Matrix
For a binary classifier with classes Positive (P) and Negative (N), the confusion matrix is a 2×2 table of prediction outcomes:
| Predicted Positive | Predicted Negative | |
|---|---|---|
| Actual Positive | True Positive (TP) | False Negative (FN) |
| Actual Negative | False Positive (FP) | True Negative (TN) |
The first word (True/False) tells you if the prediction was correct. The second word (Positive/Negative) tells you what was predicted.
- FP (Type I error) = “False alarm” — predicted positive but wrong
- FN (Type II error) = “Missed detection” — predicted negative but wrong
Q: A spam classifier on 200 emails: 120 are spam, 80 are not spam. Results: 100 spam correctly identified, 20 spam missed, 10 non-spam wrongly flagged, 70 non-spam correctly identified. Build the confusion matrix.
A: TP=100, FN=20, FP=10, TN=70. Total = 200 ✓
2. Classification Metrics
- Accuracy = (TP + TN) / (TP + TN + FP + FN)
- Precision (Positive Predictive Value) = TP / (TP + FP)
- Recall (Sensitivity, True Positive Rate) = TP / (TP + FN)
- Specificity (True Negative Rate) = TN / (TN + FP)
- F1-Score = 2 × (Precision × Recall) / (Precision + Recall) = 2TP / (2TP + FP + FN)
- Fβ-Score = (1+β²) × (Precision × Recall) / (β² × Precision + Recall)
For imbalanced datasets, accuracy is misleading. If 99% of data is Class A, predicting all as Class A gives 99% accuracy but 0% recall for Class B. Always use Precision, Recall, and F1 for imbalanced problems.
Q: Given TP=100, FN=20, FP=10, TN=70. Compute all metrics.
A:
- Accuracy = (100+70)/200 = 170/200 = 0.85
- Precision = 100/(100+10) = 100/110 ≈ 0.909
- Recall = 100/(100+20) = 100/120 ≈ 0.833
- Specificity = 70/(70+10) = 70/80 = 0.875
- F1 = 2×(0.909×0.833)/(0.909+0.833) = 2×0.757/1.742 ≈ 0.870
2.1 Precision-Recall Tradeoff
Increasing the classification threshold increases precision but decreases recall (fewer positives predicted). Decreasing the threshold increases recall but decreases precision.
- High Precision needed: Spam filtering (don’t flag legitimate email as spam)
- High Recall needed: Cancer detection (don’t miss any cancer cases)
- Fβ with β>1: Favours recall (e.g., F2 score)
- Fβ with β<1: Favours precision (e.g., F0.5 score)
3. ROC Curve and AUC
The Receiver Operating Characteristic curve plots TPR (Recall) on the y-axis vs. FPR (1 − Specificity) on the x-axis at various classification thresholds.
- TPR = TP / (TP + FN)
- FPR = FP / (FP + TN)
- AUC = 1.0 → Perfect classifier
- AUC = 0.5 → Random classifier (diagonal line)
- AUC < 0.5 → Worse than random (invert predictions)
- AUC is threshold-independent and measures overall ranking quality
- Interpretation: AUC = P(score of random positive > score of random negative)
Q: A classifier at threshold T produces TP=80, FP=20, FN=20, TN=80. Calculate the point on the ROC curve.
A: TPR = 80/100 = 0.8, FPR = 20/100 = 0.2. ROC point = (0.2, 0.8). This is above the diagonal, so better than random.
3.1 Precision-Recall Curve
For highly imbalanced datasets, the PR curve is more informative than ROC. It plots Precision vs. Recall at different thresholds.
Average Precision (AP) summarises the PR curve as the weighted mean of precisions at each threshold, with the increase in recall as the weight.
4. Regression Metrics
- MSE (Mean Squared Error) = (1/n) ∑(yᵢ − ŷᵢ)²
- RMSE (Root MSE) = √MSE — same units as y
- MAE (Mean Absolute Error) = (1/n) ∑|yᵢ − ŷᵢ|
- R² (Coefficient of Determination) = 1 − (SS_res / SS_tot)
R² = 1 − (∑(yᵢ − ŷᵢ)²) / (∑(yᵢ − ȳ)²)
- SS_res = ∑(yᵢ − ŷᵢ)² (residual sum of squares)
- SS_tot = ∑(yᵢ − ȳ)² (total sum of squares, where ȳ is the mean of y)
- R² = 1 → Perfect prediction
- R² = 0 → Model is as good as predicting the mean
- R² < 0 → Model is worse than predicting the mean
Adjusted R² = 1 − ((1−R²)(n−1)) / (n−p−1)
Where p = number of predictors, n = number of samples. Penalises adding unnecessary features. Always use Adjusted R² when comparing models with different numbers of features.
Q: Actual values: [2, 4, 6, 8]. Predicted: [2.5, 3.5, 5.5, 8.5]. Compute R².
A: Mean ȳ = (2+4+6+8)/4 = 5.
SS_res = (2−2.5)² + (4−3.5)² + (6−5.5)² + (8−8.5)² = 0.25+0.25+0.25+0.25 = 1.0
SS_tot = (2−5)² + (4−5)² + (6−5)² + (8−5)² = 9+1+1+9 = 20
R² = 1 − 1/20 = 0.95
5. Multi-Class Averaging
When there are more than two classes, per-class metrics must be aggregated:
| Method | How It Works | When to Use |
|---|---|---|
| Macro Average | Average metrics equally across all classes (unweighted mean) | When all classes are equally important |
| Micro Average | Aggregate TP, FP, FN globally, then compute metric | When overall performance matters; equals accuracy for multiclass |
| Weighted Average | Average weighted by class support (number of samples) | When class imbalance should influence the metric |
3-class classifier: Class A (P=0.9, R=0.8, support=100), Class B (P=0.7, R=0.6, support=50), Class C (P=0.8, R=0.9, support=50).
Macro Precision = (0.9+0.7+0.8)/3 = 0.8
Weighted Precision = (0.9×100 + 0.7×50 + 0.8×50)/200 = (90+35+40)/200 = 0.825
6. Metrics Summary Table
| Metric | Type | Formula | Range | GATE Frequency |
|---|---|---|---|---|
| Accuracy | Classification | (TP+TN)/(All) | [0,1] | High |
| Precision | Classification | TP/(TP+FP) | [0,1] | Very High |
| Recall | Classification | TP/(TP+FN) | [0,1] | Very High |
| F1 | Classification | Harmonic mean | [0,1] | Very High |
| AUC | Classification | Area under ROC | [0,1] | Medium |
| MSE | Regression | (1/n)∑(y−ŷ)² | [0,∞) | High |
| MAE | Regression | (1/n)∑|y−ŷ| | [0,∞) | Medium |
| R² | Regression | 1−SS_res/SS_tot | (−∞,1] | High |