how to calculate the precision and F1?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
Related reading
- How to calculate the regularization parameter in linear regression
- How to categorize True Negatives in sliding window object detection?
- How to change a learning rate for Adam in TF2?
- How to change batch size dynamically in Tensorflow 2.0 Dataset?
- How to change broadcasted ip in tomcat cluster
- How to change Keras/tensorflow version in Google colab?
- How to change size of plot in xgboost.plot_importance?
- How to change smoothing method of Naive Bayes classifier in NLTK?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.