- sklearn.metrics.f1_score(y_true, y_pred, labels=None, pos_label=1, average='weighted')¶
Compute the F1 score, also known as balanced F-score or F-measure
The F1 score can be interpreted as a weighted average of the precision and recall, where an F1 score reaches its best value at 1 and worst score at 0. The relative contribution of precision and recall to the F1 score are equal. The formula for the F1 score is:
F1 = 2 * (precision * recall) / (precision + recall)
In the multi-class case, this is the weighted average of the F1 score of each class.
y_true : array, shape = [n_samples]
Ground truth (correct) target values.
y_pred : array, shape = [n_samples]
Estimated targets as returned by a classifier.
labels : array
Integer array of labels.
pos_label : int
In the binary classification case, give the label of the positive class (default is 1). Everything else but pos_label is considered to belong to the negative class. Set to None in the case of multiclass classification.
average : string, [None, ‘micro’, ‘macro’, ‘weighted’ (default)]
In the multiclass classification case, this determines the type of averaging performed on the data.
Do not perform any averaging, return the score for each class.
Average over classes (does not take imbalance into account).
Average over instances (takes imbalance into account). This implies that precision == recall == F1.
Average weighted by support (takes imbalance into account). Can result in F-score that is not between precision and recall.
f1_score : float or array of float, shape = [n_unique_labels]
F1 score of the positive class in binary classification or weighted average of the F1 scores of each class for the multiclass task.
In the binary case:
>>> from sklearn.metrics import f1_score >>> y_pred = [0, 1, 0, 0] >>> y_true = [0, 1, 0, 1] >>> f1_score(y_true, y_pred) 0.666...
In the multiclass case:
>>> from sklearn.metrics import f1_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> f1_score(y_true, y_pred, average='macro') 0.26... >>> f1_score(y_true, y_pred, average='micro') 0.33... >>> f1_score(y_true, y_pred, average='weighted') 0.26... >>> f1_score(y_true, y_pred, average=None) array([ 0.8, 0. , 0. ])