In Python, the F1-score is a metric used to evaluate the performance of a classification model, especially when dealing with imbalanced datasets. It is the harmonic mean of precision and recall, and it can be calculated using the following formula:
[
F1 = 2 \times \frac{\text{precision} \times \text{recall}}{\text{precision} + \text{recall}}
]
Here’s how you can calculate the F1-score in Python:
1. Manual Calculation
If you already have the precision and recall values, you can calculate the F1-score manually:
def calculate_f1_score(precision, recall):
if precision + recall == 0: # To avoid division by zero
return 0
f1 = 2 * (precision * recall) / (precision + recall)
return f1
# Example usage
precision = 0.8
recall = 0.6
f1_score = calculate_f1_score(precision, recall)
print(f"F1-Score: {f1_score:.2f}")
2. Using scikit-learn
If you are working with a machine learning model, you can use the f1_score function from the scikit-learn library, which calculates the F1-score directly from true labels and predicted labels. This is more convenient when evaluating a model.
from sklearn.metrics import f1_score, precision_score, recall_score
# Example true and predicted labels
y_true = [0, 1, 1, 1, 0, 1, 0, 1]
y_pred = [0, 1, 1, 1, 0, 1, 0, 1]
# Calculate precision, recall, and F1-score
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1-Score: {f1:.2f}")
3. For Multi-Class Classification
If you are working with a multi-class classification problem, you can use the f1_score function with different averaging methods ('micro', 'macro', 'weighted', etc.) to calculate the F1-score.
from sklearn.metrics import f1_score
# Example true and predicted labels for multi-class
y_true = [0, 1, 2, 2, 0, 1]
y_pred = [0, 1, 2, 2, 0, 1]
# Calculate F1-score with different averaging methods
f1_macro = f1_score(y_true, y_pred, average='macro') # Macro average
f1_micro = f1_score(y_true, y_pred, average='micro') # Micro average
f1_weighted = f1_score(y_true, y_pred, average='weighted') # Weighted average
print(f"F1-Score (Macro): {f1_macro:.2f}")
print(f"F1-Score (Micro): {f1_micro:.2f}")
print(f"F1-Score (Weighted): {f1_weighted:.2f}")
Explanation of Averaging Methods:
- **
'micro': Calculates metrics globally by counting the total true positives, false negatives, and false positives.
- **
'macro': Calculates metrics for each class independently and then takes the unweighted mean (treats all classes equally).
- **
'weighted': Calculates metrics for each class and takes the weighted mean based on the number of instances in each class (useful for imbalanced datasets).
Notes:
- Ensure that precision and recall are not both zero to avoid division by zero in the F1-score formula.
- If you are working with a machine learning model, it’s recommended to use
scikit-learn for consistent and reliable metric calculations.
- Install
scikit-learn if you haven’t already: pip install scikit-learn.
Let me know if you need further clarification!