Skip to content

Commit f27a26d

Browse files
marijavlajicogriselglemaitreadrinjalali
authored
DOC Add examples of make_scorer usage to fbeta_score docstring (scikit-learn#28755)
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org> Co-authored-by: Guillaume Lemaitre <guillaume@probabl.ai> Co-authored-by: adrinjalali <adrin.jalali@gmail.com>
1 parent 51fae9f commit f27a26d

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

sklearn/metrics/_classification.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,11 @@ def fbeta_score(
16271627
returns 0.0 and raises ``UndefinedMetricWarning``. This behavior can be
16281628
modified by setting ``zero_division``.
16291629
1630+
F-beta score is not implemented as a named scorer that can be passed to
1631+
the `scoring` parameter of cross-validation tools directly: it requires to be
1632+
wrapped with :func:`make_scorer` so as to specify the value of `beta`. See
1633+
examples for details.
1634+
16301635
References
16311636
----------
16321637
.. [1] R. Baeza-Yates and B. Ribeiro-Neto (2011).
@@ -1650,9 +1655,29 @@ def fbeta_score(
16501655
>>> fbeta_score(y_true, y_pred, average=None, beta=0.5)
16511656
array([0.71, 0. , 0. ])
16521657
>>> y_pred_empty = [0, 0, 0, 0, 0, 0]
1653-
>>> fbeta_score(y_true, y_pred_empty,
1654-
... average="macro", zero_division=np.nan, beta=0.5)
1658+
>>> fbeta_score(
1659+
... y_true,
1660+
... y_pred_empty,
1661+
... average="macro",
1662+
... zero_division=np.nan,
1663+
... beta=0.5,
1664+
... )
16551665
0.128
1666+
1667+
In order to use :func:`fbeta_scorer` as a scorer, a callable
1668+
scorer objects needs to be created first with :func:`make_scorer`,
1669+
passing the value for the `beta` parameter.
1670+
1671+
>>> from sklearn.metrics import fbeta_score, make_scorer
1672+
>>> ftwo_scorer = make_scorer(fbeta_score, beta=2)
1673+
>>> from sklearn.model_selection import GridSearchCV
1674+
>>> from sklearn.svm import LinearSVC
1675+
>>> grid = GridSearchCV(
1676+
... LinearSVC(dual="auto"),
1677+
... param_grid={'C': [1, 10]},
1678+
... scoring=ftwo_scorer,
1679+
... cv=5
1680+
... )
16561681
"""
16571682

16581683
_, _, f, _ = precision_recall_fscore_support(

0 commit comments

Comments
 (0)