8.19.2.8. sklearn.metrics.fbeta_score¶
- sklearn.metrics.fbeta_score(y_true, y_pred, beta, labels=None, pos_label=1, average='weighted')¶
Compute the F-beta score
The F-beta score is the weighted harmonic mean of precision and recall, reaching its optimal value at 1 and its worst value at 0.
The beta parameter determines the weight of precision in the combined score. beta < 1 lends more weight to precision, while beta > 1 favors precision (beta == 0 considers only precision, beta == inf only recall).
Parameters : y_true : array-like or list of labels or label indicator matrix
Ground truth (correct) target values.
y_true : array-like or list of labels or label indicator matrix
Estimated targets as returned by a classifier.
beta: float :
Weight of precision in harmonic mean.
labels : array
Integer array of labels.
pos_label : int, 1 by default
If average is not None and the classification target is binary, only this class’s scores will be returned. In multilabel classification, it is used to infer what is a positive label in the label indicator matrix format.
average : string, [None, ‘micro’, ‘macro’, ‘samples’, ‘weighted’ (default)]
If None, the scores for each class are returned. Otherwise, unless pos_label is given in binary classification, this determines the type of averaging performed on the data:
- 'samples':
Average over instance. Only meaningful and available in multilabel classification.
- 'macro':
Average over classes (does not take imbalance into account).
- 'micro':
Aggregate classes and average over instances (takes imbalance into account). This implies that precision == recall == F1. In multilabel classification, this is true only if every sample has a label.
- 'weighted':
Average over classes weighted by support (takes imbalance into account). Can result in F-score that is not between precision and recall.
Returns : fbeta_score : float (if average is not None) or array of float, shape = [n_unique_labels]
F-beta score of the positive class in binary classification or weighted average of the F-beta score of each class for the multiclass task.
References
[R127] R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 327-328. [R128] Wikipedia entry for the F1-score Examples
In the binary case:
>>> from sklearn.metrics import fbeta_score >>> y_pred = [0, 1, 0, 0] >>> y_true = [0, 1, 0, 1] >>> fbeta_score(y_true, y_pred, beta=0.5) 0.83... >>> fbeta_score(y_true, y_pred, beta=1) 0.66... >>> fbeta_score(y_true, y_pred, beta=2) 0.55...
In the multiclass case:
>>> from sklearn.metrics import fbeta_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> fbeta_score(y_true, y_pred, average='macro', beta=0.5) ... 0.23... >>> fbeta_score(y_true, y_pred, average='micro', beta=0.5) ... 0.33... >>> fbeta_score(y_true, y_pred, average='weighted', beta=0.5) ... 0.23... >>> fbeta_score(y_true, y_pred, average=None, beta=0.5) ... array([ 0.71..., 0. , 0. ])
In the multilabel case with binary indicator format:
>>> from sklearn.metrics import fbeta_score >>> y_true = np.array([[0.0, 1.0, 0.0], [1.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) >>> y_pred = np.ones((3, 3)) >>> fbeta_score(y_true, y_pred, average='macro', beta=0.5) ... 0.49... >>> fbeta_score(y_true, y_pred, average='micro', beta=0.5) 0.5 >>> fbeta_score(y_true, y_pred, average='weighted', beta=0.5) ... 0.54... >>> fbeta_score(y_true, y_pred, average='samples', beta=0.5) ... 0.66... >>> fbeta_score(y_true, y_pred, average=None, beta=0.5) ... array([ 0.38..., 0.71..., 0.38...])
and with a list of labels format:
>>> from sklearn.metrics import fbeta_score >>> y_true = [(1, 2), (3,)] >>> y_pred = [(1, 2), tuple()] >>> fbeta_score(y_true, y_pred, average='macro', beta=0.5) ... 0.66... >>> fbeta_score(y_true, y_pred, average='micro', beta=0.5) ... 0.90... >>> fbeta_score(y_true, y_pred, average='weighted', beta=0.5) ... 0.66... >>> fbeta_score(y_true, y_pred, average='samples', beta=0.5) ... 0.42... >>> fbeta_score(y_true, y_pred, average=None, beta=0.5) array([ 1., 1., 0.])