How to calculate F1 Score in Python?
The f1_score function from the sklearn.metrics module is used to calculate the F1 score for a multi-class classification problem. The function takes two required parameters, y_true and y_pred, and an optional parameter average. Here’s an explanation of the function and its parameters:
Python3
from sklearn.metrics import f1_score f1_score(y_true, y_pred, average = None ) |
- y_true (array-like of shape (n_samples,)): This parameter represents the true labels or ground truth for the samples. It should be an array-like structure (e.g., a list or NumPy array) containing the true class labels for each sample in the dataset.
- y_pred (array-like of shape (n_samples,)): This parameter represents the predicted labels for the samples. Like y_true, it should be an array-like structure containing the predicted class labels for each sample.
- average (string or None, default=None): This is an optional parameter that specifies the method used to calculate the F1 score for multi-class classification. It can take the following values:
- None: Returns the F1 score for each class separately. In this case, the function returns an array of shapes (n_classes,).
- ‘micro’: Calculates the F1 score globally by considering total true positives, false positives, and false negatives across all classes.
- ‘macro’: Calculates the F1 score for each class independently and then computes the unweighted average.
- ‘weighted’: Calculates the F1 score for each class independently and then computes the average weighted by the number of true instances in each class.
Python3
from sklearn.metrics import f1_score # Example data y_true = [ 0 , 1 , 2 , 2 , 2 , 2 , 1 , 0 , 2 , 1 , 0 ] y_pred = [ 0 , 0 , 2 , 2 , 1 , 2 , 1 , 0 , 1 , 2 , 1 ] # Calculate F1 score for each class separately f1_per_class = f1_score(y_true, y_pred, average = None ) # Calculate micro-average F1 score f1_micro = f1_score(y_true, y_pred, average = 'micro' ) # Calculate macro-average F1 score f1_macro = f1_score(y_true, y_pred, average = 'macro' ) # Calculate weighted-average F1 score f1_weighted = f1_score(y_true, y_pred, average = 'weighted' ) print ( "F1 score per class:" , f1_per_class) print ( "Micro-average F1 score:" , f1_micro) print ( "Macro-average F1 score:" , f1_macro) print ( "Weighted-average F1 score:" , f1_weighted) |
Output:
F1 score per class: [0.66666667 0.28571429 0.66666667]
Micro-average F1 score: 0.5454545454545454
Macro-average F1 score: 0.5396825396825397
Weighted-average F1 score: 0.5627705627705627
F1 Score in Machine Learning
The F1 score is an important evaluation metric that is commonly used in classification tasks to evaluate the performance of a model. It combines precision and recall into a single value. In this article, we will understand in detail how the F1 score is calculated and compare it with other metrics.