how to calculate the precision and F1?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In the realm of machine learning, especially in classification tasks, evaluating the performance of a model is crucial. Precision and the F1 score are two significant performance metrics that give insights beyond mere accuracy. These metrics are particularly useful when dealing with imbalanced datasets. This article explains how to calculate precision and F1 score, providing technical insights and examples for better understanding.
Confusion Matrix
Before diving into precision and F1 score, it's essential to understand the confusion matrix, which helps visualize the performance of an algorithm. For a binary classification problem, the confusion matrix consists of four components:
• True Positive (TP): Predictions where the model correctly predicted the positive class. • True Negative (TN): Predictions where the model correctly predicted the negative class. • False Positive (FP): Predictions where the model incorrectly predicted the positive class (Type I error). • False Negative (FN): Predictions where the model incorrectly predicted the negative class (Type II error).
Precision
Precision is the ratio of correctly predicted positive observations to the total predicted positives. It answers the question: "Of all the positive predictions, how many were truly positive?"
Formula
The formula for precision is:
Interpretation
• A precision of 1 (or 100%) implies that every positive prediction was correct. • A precision of 0 implies that none of the positive predictions were correct.
Example
Consider a binary classification with the following confusion matrix:
| Predicted Positive | Predicted Negative | |
| Actual Positive | 40 | 10 |
| Actual Negative | 5 | 45 |
Here, we have:
• •
The precision is calculated as:
This indicates that 89% of the predictions that were positive are truly positive.
F1 `Score`
The F1 score is the harmonic mean of precision and recall. It provides a balance between precision and recall, which is useful when you need a single metric to evaluate a model's performance, especially with imbalanced classes.
Formula
The formula for the F1 score is:
Recall is the ratio of correctly predicted positive observations to the total actual positive observations and is given by:
Interpretation
• An F1 score of 1 (or 100%) indicates a perfect balance between precision and recall. • An F1 score of 0 indicates the poorest performance.
Example Calculation
Continuing from the previous example, let's also calculate recall:
•
Now, using both precision and recall, we calculate the F1 score:
This indicates a good balance between precision and recall in the model's performance.
Key Points Summary
| Metric | Definition | Formula | Range |
| Precision | Ratio of true positive results in all positive predictions | [0, 1] | |
| Recall | Ratio of true positive results in all actual positives | [0, 1] | |
F1 Score | Harmonic mean of precision and recall | [0, 1] |
Conclusion
Precision and F1 scores are crucial metrics when evaluating the performance of classification models, particularly when dealing with imbalanced datasets. Precision highlights the accuracy of positive predictions, while the F1 score offers a balanced measure considering both precision and recall. Understanding and accurately calculating these metrics help in fine-tuning models and achieving better predictive performance.

