From e1e64a6cb4292d32575733030baefdbb97c32c07 Mon Sep 17 00:00:00 2001 From: Nawazish Alam Date: Tue, 7 Feb 2023 13:30:08 +0530 Subject: [PATCH 1/6] added validate_params to f1_score --- sklearn/metrics/_classification.py | 20 +++++++++++++++++++- sklearn/tests/test_public_functions.py | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/sklearn/metrics/_classification.py b/sklearn/metrics/_classification.py index c90ea6f86868b..22748634f0a3e 100644 --- a/sklearn/metrics/_classification.py +++ b/sklearn/metrics/_classification.py @@ -23,6 +23,7 @@ # License: BSD 3 clause +from numbers import Integral, Real import warnings import numpy as np @@ -40,7 +41,7 @@ from ..utils.multiclass import type_of_target from ..utils.validation import _num_samples from ..utils.sparsefuncs import count_nonzero -from ..utils._param_validation import StrOptions, validate_params +from ..utils._param_validation import StrOptions, Interval, validate_params from ..exceptions import UndefinedMetricWarning from ._base import _check_pos_label_consistency @@ -1038,6 +1039,23 @@ def zero_one_loss(y_true, y_pred, *, normalize=True, sample_weight=None): return n_samples - score +@validate_params( + { + "y_true": ["array-like", "sparse matrix"], + "y_pred": ["array-like", "sparse matrix"], + "labels": ["array-like", None], + "pos_label": [Integral, str], + "average": [ + StrOptions({"micro", "macro", "samples", "weighted", "binary"}), + None, + ], + "sample_weight": ["array-like", None], + "zero_division": [ + Interval(Integral, 0, 1, closed="both"), + StrOptions({"warn"}), + ], + } +) def f1_score( y_true, y_pred, diff --git a/sklearn/tests/test_public_functions.py b/sklearn/tests/test_public_functions.py index 4a733b1b71252..acfb12a83fc35 100644 --- a/sklearn/tests/test_public_functions.py +++ b/sklearn/tests/test_public_functions.py @@ -128,6 +128,7 @@ def _check_function_param_validation( "sklearn.metrics.r2_score", "sklearn.metrics.roc_curve", "sklearn.metrics.zero_one_loss", + "sklearn.metrics.f1_score", "sklearn.model_selection.train_test_split", "sklearn.random_projection.johnson_lindenstrauss_min_dim", "sklearn.svm.l1_min_c", From 12ff0122ed938044db3bfe3e32d346a248e70bd2 Mon Sep 17 00:00:00 2001 From: Nawazish Alam Date: Tue, 7 Feb 2023 13:58:16 +0530 Subject: [PATCH 2/6] added validate_params to f1_score --- sklearn/tests/test_public_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/tests/test_public_functions.py b/sklearn/tests/test_public_functions.py index acfb12a83fc35..25bd737ce8455 100644 --- a/sklearn/tests/test_public_functions.py +++ b/sklearn/tests/test_public_functions.py @@ -117,6 +117,7 @@ def _check_function_param_validation( "sklearn.metrics.cohen_kappa_score", "sklearn.metrics.confusion_matrix", "sklearn.metrics.det_curve", + "sklearn.metrics.f1_score", "sklearn.metrics.hamming_loss", "sklearn.metrics.mean_absolute_error", "sklearn.metrics.mean_squared_error", @@ -128,7 +129,6 @@ def _check_function_param_validation( "sklearn.metrics.r2_score", "sklearn.metrics.roc_curve", "sklearn.metrics.zero_one_loss", - "sklearn.metrics.f1_score", "sklearn.model_selection.train_test_split", "sklearn.random_projection.johnson_lindenstrauss_min_dim", "sklearn.svm.l1_min_c", From f9e5604a7307e476a425704d5d8a55d5e5dc51d8 Mon Sep 17 00:00:00 2001 From: Nawazish Alam Date: Tue, 7 Feb 2023 15:59:56 +0530 Subject: [PATCH 3/6] added validate_params to f1_score --- sklearn/metrics/_classification.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/metrics/_classification.py b/sklearn/metrics/_classification.py index 22748634f0a3e..42b24def3b896 100644 --- a/sklearn/metrics/_classification.py +++ b/sklearn/metrics/_classification.py @@ -23,7 +23,7 @@ # License: BSD 3 clause -from numbers import Integral, Real +from numbers import Integral import warnings import numpy as np @@ -1044,7 +1044,7 @@ def zero_one_loss(y_true, y_pred, *, normalize=True, sample_weight=None): "y_true": ["array-like", "sparse matrix"], "y_pred": ["array-like", "sparse matrix"], "labels": ["array-like", None], - "pos_label": [Integral, str], + "pos_label": [Integral, str, None], "average": [ StrOptions({"micro", "macro", "samples", "weighted", "binary"}), None, From 456a637d3e0528f6dd7ada5b9d87f2773f657c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= <34657725+jeremiedbb@users.noreply.github.com> Date: Wed, 8 Feb 2023 18:38:35 +0100 Subject: [PATCH 4/6] Update _classification.py --- sklearn/metrics/_classification.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sklearn/metrics/_classification.py b/sklearn/metrics/_classification.py index d05a4af0789a2..22539c79db12a 100644 --- a/sklearn/metrics/_classification.py +++ b/sklearn/metrics/_classification.py @@ -41,7 +41,7 @@ from ..utils.multiclass import type_of_target from ..utils.validation import _num_samples from ..utils.sparsefuncs import count_nonzero -from ..utils._param_validation import StrOptions, Interval, validate_params +from ..utils._param_validation import StrOptions, Options, validate_params from ..exceptions import UndefinedMetricWarning from ._base import _check_pos_label_consistency @@ -1044,14 +1044,14 @@ def zero_one_loss(y_true, y_pred, *, normalize=True, sample_weight=None): "y_true": ["array-like", "sparse matrix"], "y_pred": ["array-like", "sparse matrix"], "labels": ["array-like", None], - "pos_label": [Integral, str, None], + "pos_label": [Real, str, "boolean"], "average": [ StrOptions({"micro", "macro", "samples", "weighted", "binary"}), None, ], "sample_weight": ["array-like", None], "zero_division": [ - Interval(Integral, 0, 1, closed="both"), + Options(Integral, {0, 1}), StrOptions({"warn"}), ], } From a1019cb6784cfab7af3c6e475db5d7c94d650bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= <34657725+jeremiedbb@users.noreply.github.com> Date: Wed, 8 Feb 2023 18:44:47 +0100 Subject: [PATCH 5/6] Update _classification.py --- sklearn/metrics/_classification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/metrics/_classification.py b/sklearn/metrics/_classification.py index 22539c79db12a..7536898c5e3cf 100644 --- a/sklearn/metrics/_classification.py +++ b/sklearn/metrics/_classification.py @@ -23,7 +23,7 @@ # License: BSD 3 clause -from numbers import Integral +from numbers import Integral, Real import warnings import numpy as np From 4257a634555d69775ccb310cf0f29767d766adc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20du=20Boisberranger?= <34657725+jeremiedbb@users.noreply.github.com> Date: Wed, 8 Feb 2023 19:08:25 +0100 Subject: [PATCH 6/6] Update _classification.py --- sklearn/metrics/_classification.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/metrics/_classification.py b/sklearn/metrics/_classification.py index 7536898c5e3cf..bde3773a573a0 100644 --- a/sklearn/metrics/_classification.py +++ b/sklearn/metrics/_classification.py @@ -1044,7 +1044,7 @@ def zero_one_loss(y_true, y_pred, *, normalize=True, sample_weight=None): "y_true": ["array-like", "sparse matrix"], "y_pred": ["array-like", "sparse matrix"], "labels": ["array-like", None], - "pos_label": [Real, str, "boolean"], + "pos_label": [Real, str, "boolean", None], "average": [ StrOptions({"micro", "macro", "samples", "weighted", "binary"}), None, @@ -1101,7 +1101,7 @@ def f1_score( .. versionchanged:: 0.17 Parameter `labels` improved for multiclass problem. - pos_label : int, float, bool or str, default=1 + pos_label : int, float, bool, str or None, default=1 The class to report if ``average='binary'`` and the data is binary. If the data are multiclass or multilabel, this will be ignored; setting ``labels=[pos_label]`` and ``average != 'binary'`` will report