fbeta-score
Metric
fbeta_scorefromscikit-learn(sklearn.metrics.fbeta_score)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with fbeta_score, or
mentions sklearn.metrics.fbeta_score directly, or wants the standard scikit-learn implementation.
Reference signature
from sklearn.metrics import fbeta_score
# fbeta_score(y_true, y_pred, *, beta, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')
Library docstring
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 represents the ratio of recall importance to
precision importance. `beta > 1` gives more weight to recall, while
`beta < 1` favors precision. For example, `beta = 2` makes recall twice
as important as precision, while `beta = 0.5` does the opposite.
Asymptotically, `beta -> +inf` considers only recall, and `beta -> 0`
only precision.
The formula for F-beta score is:
.. math::
F_\beta = \frac{(1 + \beta^2) \text{tp}}
{(1 + \beta^2) \text{tp} + \text{fp} + \beta^2 \text{fn}}
Where :math:`\text{tp}` is the number of true positives, :math:`\text{fp}` is the
number of false positives, and :math:`\text{fn}` is the number of false negatives.
Support beyond :term:`binary` targets is achieved by treating :term:`multiclass`
and :term:`multilabel` data as a collection of binary problems, one for each
label. For the :term:`binary` case, setting `average='binary'` will return
F-beta score for `pos_label`. If `average` is not `'binary'`, `pos_label` is
ignored and F-beta score for both classes are computed, then averaged or both
returned (when `average=None`). Similarly, for :term:`multiclass` and
:term:`multilabel` targets, F-beta score for all `labels` are either returned or
averaged depending on the `average` parameter. Use `labels` specify the set of
labels to calculate F-beta score for.
Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.
Parameters
----------
y_true : 1d array-like, or label indicator array / sparse matrix
Ground truth (correct) target values. Sparse matrix is only supported when
targets are of :term:`multilabel` type.
y_pred : 1d array-like, or label indicator array / sparse matrix
Estimated targets as returned by a classifier. Sparse matrix is only
supported when targets are of :term:`multilabel` type.
beta : float
Determines the weight of recall in the combined score.
labels : array-like, default=None
The set of labels to include when `average != 'binary'`, and their
order if `average is None`.
Quick recipe
import sklearn.metrics as _m
score = _m.fbeta_score(y_true, y_pred)
Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is
(y_true, y_pred)while torchmetrics is(preds, target).