In your case, where all predictions and actual values are zero, calculating the F1 score results in division by zero because both precision and recall are undefined. Here’s the breakdown:
- True Positives (TP): 0
- False Positives (FP): 0
- False Negatives (FN): 0
Calculations:
- Precision is undefined since TP and FP are both 0.
- Recall is undefined since TP and FN are both 0.
- Consequently, the F1 score is also undefined.
Conclusion:
In this scenario, it is common to define the F1 score as 0, as the model has not identified any positive instances correctly. Many libraries may return either nan
or 0 for the F1 score in such cases. It’s good practice to handle this specifically in your evaluation metrics by returning 0 when both precision and recall are undefined.
Another example to go further:
In this example, we will consider a scenario where we add one to each category of the classification results. Specifically, we will assume:
- True Positives (TP): 0 → becomes 1 after the adjustment.
- False Positives (FP): 0 → becomes 1 after the adjustment.
- False Negatives (FN): 0 → becomes 1 after the adjustment.
- True Negatives (TN): We will also assume there are 1,000 true negatives.
This will allow us to evaluate the impact on the F1 score and other metrics under these conditions.
Calculating the Metrics:
Precision:
- Precision measures how many of the predicted positive cases were actually positive. It's calculated as:
Precision = TP / (TP + FP) = 1 / (1 + 1) = 0.5
This means that half of the predicted positive cases were correct.
Recall:
- Recall measures how many actual positive cases were identified by the model. It’s calculated as:
Recall = TP / (TP + FN) = 1 / (1 + 1) = 0.5
This indicates that the model successfully identified half of the actual positive cases.
F1 Score:
- The F1 score is the harmonic mean of precision and recall, providing a single score that balances both metrics. It can be calculated as:
F1 = 2 * (Precision * Recall) / (Precision + Recall) = 2 * (0.5 * 0.5) / (0.5 + 0.5) = 0.5
So, the F1 score in this case is 0.5.
The Role of True Negatives:
While you have 1,000 true negatives in this scenario, it’s important to note that true negatives do not impact the F1 score directly. The F1 score is primarily concerned with how well the model identifies the positive class (i.e., true positives, false positives, and false negatives). True negatives contribute to overall accuracy but not to the F1 score.
Conclusion:
In summary, even with a substantial number of true negatives, the adjustments made to the counts (adding +1 to TP, FP, and FN) lead to an F1 score of 0.5. This score reflects the balance between precision and recall for the positive class. While true negatives are essential for understanding model performance overall, they don’t influence the F1 score, which focuses on the model’s ability to correctly identify positive cases.