ML classification metrics & evaluation • 2026 edition
\( \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} \)
\( \text{Precision} = \frac{TP}{TP + FP} \)
\( \text{Recall} = \frac{TP}{TP + FN} \)
\( \text{F1-Score} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} \)
\( \text{Specificity} = \frac{TN}{TN + FP} \)
Where:
Additional metrics:
Example: For a binary classifier with 90 TP, 85 TN, 5 FP, and 10 FN:
\( \text{Accuracy} = \frac{90 + 85}{90 + 85 + 5 + 10} = \frac{175}{190} = 0.921 \) or 92.1%
\( \text{Precision} = \frac{90}{90 + 5} = \frac{90}{95} = 0.947 \) or 94.7%
\( \text{Recall} = \frac{90}{90 + 10} = \frac{90}{100} = 0.900 \) or 90.0%
The model has an accuracy of 92.1%, precision of 94.7%, and recall of 90.0%.
| Metric | Value | Quality |
|---|---|---|
| Accuracy | 92.1% | Excellent |
| Precision | 94.7% | Excellent |
| Recall | 90.0% | Good |
| F1-Score | 92.3% | Excellent |
A confusion matrix is a table used to describe the performance of a classification model on a set of test data for which the true values are known. It provides a detailed breakdown of correct and incorrect predictions, organized by class. The matrix allows for detailed analysis of classification performance beyond simple accuracy metrics.
The confusion matrix for binary classification:
Where:
Choose metrics based on your problem requirements:
Table showing true vs predicted classifications.
\( \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} \)
\( \text{Precision} = \frac{TP}{TP + FP} \)
\( \text{Recall} = \frac{TP}{TP + FN} \)
\( \text{F1-Score} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} \)
Systematic approach to assessing model effectiveness and reliability.
What does a False Positive (FP) represent in a confusion matrix?
The answer is B) A case that was incorrectly predicted as positive. A False Positive occurs when the model predicts the positive class, but the actual class is negative. This is also known as a Type I error. For example, in medical testing, a False Positive would be diagnosing someone as having a disease when they are actually healthy.
Students must understand that the "false" in False Positive refers to the prediction being incorrect, not the actual class. The "positive" refers to the predicted class. So a False Positive is a negative case (healthy person) that was predicted as positive (predicted as diseased). This terminology can be confusing initially, but remembering "predicted vs actual" helps clarify the meaning.
False Positive (FP): Predicted positive but actually negative
Type I Error: Rejecting a true null hypothesis
Actual vs Predicted: True class vs model prediction
• "False" refers to prediction correctness
• "Positive/Negative" refers to predicted class
• Always consider both predicted and actual classes
• Remember: Predicted vs Actual
• FP = Predicted Positive, Actually Negative
• Think of medical testing examples
• Confusing predicted vs actual class
• Thinking "false" refers to actual class
• Mixing up False Positive and False Negative
Given a binary classifier with 80 TP, 70 TN, 15 FP, and 5 FN, calculate the accuracy, precision, and recall. Show your work.
Given: TP=80, TN=70, FP=15, FN=5
Accuracy calculation:
\( \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} = \frac{80 + 70}{80 + 70 + 15 + 5} = \frac{150}{170} = 0.882 \) or 88.2%
Precision calculation:
\( \text{Precision} = \frac{TP}{TP + FP} = \frac{80}{80 + 15} = \frac{80}{95} = 0.842 \) or 84.2%
Recall calculation:
\( \text{Recall} = \frac{TP}{TP + FN} = \frac{80}{80 + 5} = \frac{80}{85} = 0.941 \) or 94.1%
The model has an accuracy of 88.2%, precision of 84.2%, and recall of 94.1%.
This calculation demonstrates how different metrics can vary for the same model. The recall (94.1%) is higher than precision (84.2%), indicating the model is better at identifying positive cases than avoiding false positives. This pattern is common in models that are optimized for sensitivity rather than specificity.
Accuracy: Overall correctness of predictions
Precision: Proportion of positive predictions that are correct
Recall: Proportion of actual positives correctly identified
• Always use the correct formula for each metric
• Convert fractions to percentages for better interpretation
• Round to appropriate decimal places
• Remember: Precision = TP/(TP+FP)
• Recall = TP/(TP+FN)
• Accuracy = (TP+TN)/(TP+TN+FP+FN)
• Confusing precision and recall formulas
• Forgetting to include all components in accuracy
• Arithmetic errors in the calculations
A medical diagnostic test for a rare disease has the following confusion matrix: TP=95, TN=980, FP=15, FN=5. Calculate the sensitivity (recall) and specificity. If the disease affects 1% of the population, discuss the practical implications of these metrics for the test.
Given: TP=95, TN=980, FP=15, FN=5
Sensitivity (Recall) calculation:
\( \text{Sensitivity} = \frac{TP}{TP + FN} = \frac{95}{95 + 5} = \frac{95}{100} = 0.95 \) or 95%
Specificity calculation:
\( \text{Specificity} = \frac{TN}{TN + FP} = \frac{980}{980 + 15} = \frac{980}{995} = 0.985 \) or 98.5%
Practical implications:
Despite high sensitivity and specificity, the low prevalence means that a significant portion of positive results are false positives.
This example demonstrates the importance of considering prevalence rates in addition to sensitivity and specificity. Even with excellent sensitivity and specificity, low prevalence can lead to a high false positive rate. This is why screening tests often have follow-up confirmatory tests to reduce the impact of false positives in low-prevalence populations.
Sensitivity: Proportion of actual positives correctly identified
Specificity: Proportion of actual negatives correctly identified
Prevalence: Proportion of population with condition
• Consider prevalence when interpreting positive results
• High sensitivity is important for screening tests
• High specificity reduces false positives
• Calculate positive predictive value for low prevalence
• Consider both sensitivity and specificity
• Think about practical implications of errors
• Not considering prevalence in interpretation
• Focusing only on sensitivity or specificity
• Ignoring the impact of false positives
An email service has developed a spam filter with the following confusion matrix: TP=950, TN=8950, FP=50, FN=50. Calculate precision, recall, and F1-score. If the cost of blocking an important email is 10 times the cost of letting spam through, which metric should the engineers prioritize to optimize?
Given: TP=950, TN=8950, FP=50, FN=50
Precision calculation:
\( \text{Precision} = \frac{TP}{TP + FP} = \frac{950}{950 + 50} = \frac{950}{1000} = 0.95 \) or 95%
Recall calculation:
\( \text{Recall} = \frac{TP}{TP + FN} = \frac{950}{950 + 50} = \frac{950}{1000} = 0.95 \) or 95%
F1-Score calculation:
\( \text{F1-Score} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} = 2 \times \frac{0.95 \times 0.95}{0.95 + 0.95} = \frac{1.805}{1.9} = 0.95 \) or 95%
Analysis:
Engineers should prioritize precision to minimize false positives (blocking important emails). In this case, they should tune the model to reduce FP even if it slightly increases FN.
This problem illustrates how domain-specific costs can influence metric selection. In spam detection, false positives (important emails blocked) are typically much more costly than false negatives (spam getting through). Understanding the business impact of different error types is crucial for model optimization decisions.
False Positive Cost: Cost of incorrectly predicting positive class
False Negative Cost: Cost of incorrectly predicting negative class
Cost-Sensitive Learning: Optimizing based on error costs
• Consider domain-specific costs of errors
• Prioritize metrics that minimize high-cost errors
• Balance precision and recall based on cost structure
• Calculate actual cost impact of errors
• Prioritize metrics based on business requirements
• Consider cost-sensitive learning approaches
• Not considering the real-world cost of different errors
• Focusing only on numerical metrics
• Ignoring business impact in model optimization
When is the F1-score particularly valuable as an evaluation metric?
The answer is B) When you need to balance precision and recall. The F1-score is the harmonic mean of precision and recall, making it particularly useful when you want to consider both metrics equally. It's especially valuable in imbalanced datasets where accuracy can be misleading, and you need a single metric that captures both the ability to identify positive cases (recall) and the reliability of positive predictions (precision).
Students must understand that F1-score provides a single metric that balances precision and recall. It's particularly useful when there's no clear preference between precision and recall, or when dealing with imbalanced datasets where a single metric is needed to evaluate performance comprehensively. The harmonic mean ensures that both precision and recall must be reasonably high for a good F1-score.
F1-Score: Harmonic mean of precision and recall
Harmonic Mean: Average that gives equal weight to both values
Imbalanced Dataset: Dataset with unequal class distribution
• F1-score balances precision and recall
• Particularly useful for imbalanced data
• Range from 0 to 1, higher is better
• Use F1 when both precision and recall matter
• Better than accuracy for imbalanced datasets
• Favors models that perform well on both metrics
• Thinking F1-score is always the best metric
• Not understanding when to prioritize precision vs recall
• Confusing F1-score with simple average of precision and recall
Q: How do I interpret a confusion matrix and what insights can I gain from it?
A: A confusion matrix provides detailed insights into your model's performance:
Reading the Matrix:
Key Insights:
Practical Applications:
The confusion matrix is essential for understanding not just whether your model is accurate, but why it makes mistakes and how to improve it.
Q: What's the difference between precision, recall, and F1-score, and when should I optimize for each?
A: These metrics measure different aspects of classification performance:
Precision: Of all positive predictions, how many were correct?
Formula: \( \text{Precision} = \frac{TP}{TP + FP} \)
Measures the reliability of positive predictions. High precision means fewer false alarms.
Recall (Sensitivity): Of all actual positives, how many did we correctly identify?
Formula: \( \text{Recall} = \frac{TP}{TP + FN} \)
Measures the completeness of positive identification. High recall means fewer misses.
F1-Score: Harmonic mean of precision and recall
Formula: \( \text{F1} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}} \)
Provides a single metric that balances both precision and recall.
When to Optimize for Each:
There's typically a trade-off between precision and recall - improving one often degrades the other. The optimal choice depends on your specific application and the relative costs of different types of errors.