Precision, Recall, and F1 Score
Compute precision, recall, and F1 score for binary classification.
Given lists of true and predicted labels:
Precision=TP+FPTP​ Recall=TP+FNTP​ F1=2⋅Precision+RecallPrecision⋅Recall​
Return a tuple (precision, recall, f1), each rounded to 4 decimal places. If a denominator is 0, return 0.0 for that metric.
Example:
y_true = [1, 0, 1, 1, 0, 1] y_pred = [1, 0, 0, 1, 1, 1]
(0.75, 0.75, 0.75)
- First, we identify the true positives (TP), false positives (FP), and false negatives (FN) by comparing
y_trueandy_pred: TP=3, FP=1, FN=1 - Then, we calculate precision and recall using the given formulas: Precision=3+13​=43​=0.75, Recall=3+13​=43​=0.75
- Next, we calculate the F1 score using the precision and recall values: F1=2⋅0.75+0.750.75⋅0.75​=2⋅1.50.5625​=0.75
- The final output is a tuple of the calculated metrics, each rounded to 4 decimal places: (0.75,0.75,0.75)
Constraints:
- y_true and y_pred are lists of 0s and 1s
- Return tuple (precision, recall, f1) rounded to 4 decimal places
- Handle edge case where denominator is 0 (return 0.0)
Background Knowledge
In machine learning, evaluating the performance of a model is crucial to understanding its strengths and weaknesses. For binary classification problems, where the goal is to predict one of two classes (e.g., 0 or 1, yes or no), precision, recall, and F1 score are essential metrics. Precision measures the proportion of true positives among all predicted positive instances, indicating how accurate the model is when it predicts a positive outcome. Recall, on the other hand, measures the proportion of true positives among all actual positive instances, reflecting the model's ability to detect all instances of the positive class.
The F1 score is the harmonic mean of precision and recall, providing a balanced measure of both. It's particularly useful when the classes are imbalanced, meaning one class has a significantly larger number of instances than the other. Understanding these metrics is fundamental because they help in tuning the model's performance based on the specific requirements of the problem. For instance, in medical diagnosis, recall might be more critical to ensure that all potential cases are identified, even if it means some false positives are included.
The formulas for precision, recall, and F1 score are based on the counts of true positives (TP), false positives (FP), and false negatives (FN). True positives are instances that are correctly predicted as positive, false positives are instances that are incorrectly predicted as positive, and false negatives are instances that are incorrectly predicted as negative. These counts can be derived from a confusion matrix, a table used to evaluate the performance of a classification model.
Algorithm/Approach
The general approach to solving this problem involves calculating the counts of true positives, false positives, and false negatives by comparing the true labels with the predicted labels. Then, use these counts to compute precision, recall, and F1 score according to the given formulas. It's essential to handle the cases where the denominators in the formulas are zero to avoid division by zero errors.
Step-by-Step Strategy
- Initialize counters for true positives (TP), false positives (FP), and false negatives (FN) to zero.
- Iterate through the lists of true and predicted labels simultaneously.
- For each pair of true and predicted labels:
- If both are positive, increment TP.
- If the true label is negative but the predicted label is positive, increment FP.
- If the true label is positive but the predicted label is negative, increment FN.
- Calculate precision using the formula Precision=TP+FPTP​, handling the case where TP+FP=0.
- Calculate recall using the formula Recall=TP+FNTP​, handling the case where TP+FN=0.
- Calculate the F1 score using the formula F1=2⋅Precision+RecallPrecision⋅Recall​, handling the case where Precision+Recall=0.
- Round each of precision, recall, and F1 score to 4 decimal places.
Common Pitfalls
- Not handling the cases where the denominators in the formulas for precision, recall, or F1 score are zero.
- Incorrectly counting true positives, false positives, and false negatives.
- Not rounding the final values of precision, recall, and F1 score to 4 decimal places as required.
Time & Space Complexity
The time complexity of this solution is O(n), where n is the number of labels, because we iterate through the lists of true and predicted labels once. The space complexity is O(1), as we use a constant amount of space to store the counts of true positives, false positives, and false negatives, regardless of the size of the input.