8.17.1.7. sklearn.metrics.f1_score¶
- sklearn.metrics.f1_score(y_true, y_pred, labels=None, pos_label=1, average='weighted')¶
Compute f1 score
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 formular for the F_1 score is:
F_1 = 2 * (precision * recall) / (precision + recall)
See: http://en.wikipedia.org/wiki/F1_score
In the multi-class case, this is the weighted average of the f1-score of each class.
Parameters : y_true : array, shape = [n_samples]
True targets
y_pred : array, shape = [n_samples]
Predicted targets
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.
- macro:
Average over classes (does not take imbalance into account).
- micro:
Average over instances (takes imbalance into account). This implies that precision == recall == f1
- weighted:
Average weighted by support (takes imbalance into account). Can result in f1 score that is not between precision and recall.
Returns : f1_score : float
f1_score of the positive class in binary classification or weighted average of the f1_scores of each class for the multiclass task
References