From 56175d6cb86e093ecab134f718f9b4c1178ce3f5 Mon Sep 17 00:00:00 2001 From: Barata Tripramudya Onggo Date: Sun, 23 Apr 2023 22:36:07 +0900 Subject: [PATCH 1/5] Deprecate `matching` as metric --- doc/modules/neighbors.rst | 2 +- sklearn/metrics/_dist_metrics.pyx.tp | 4 +++- sklearn/metrics/pairwise.py | 8 ++++++-- sklearn/neighbors/_ball_tree.pyx | 3 +-- sklearn/neighbors/_base.py | 4 +++- sklearn/neighbors/tests/test_ball_tree.py | 1 - 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 7112c2a697651..ba8ff5747025f 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -142,7 +142,7 @@ of valid metrics use :meth:`KDTree.valid_metrics` and :meth:`BallTree.valid_metr >>> KDTree.valid_metrics() ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity'] >>> BallTree.valid_metrics() - ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', 'jaccard', 'dice', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', 'haversine', 'pyfunc'] + ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'jaccard', 'dice', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', 'haversine', 'pyfunc'] .. _classification: diff --git a/sklearn/metrics/_dist_metrics.pyx.tp b/sklearn/metrics/_dist_metrics.pyx.tp index ed09552ed5914..2dde3e8d9343c 100644 --- a/sklearn/metrics/_dist_metrics.pyx.tp +++ b/sklearn/metrics/_dist_metrics.pyx.tp @@ -50,7 +50,6 @@ def newObj(obj): BOOL_METRICS = [ "hamming", - "matching", "jaccard", "dice", "rogerstanimoto", @@ -61,6 +60,9 @@ BOOL_METRICS = [ if sp_base_version < parse_version("1.11"): # Deprecated in SciPy 1.9 and removed in SciPy 1.11 BOOL_METRICS += ["kulsinski"] +if sp_base_version < parse_version("1.9"): + # Deprecated in SciPy 1.0 and removed in SciPy 1.9 + BOOL_METRICS += ["matching"] def get_valid_metric_ids(L): """Given an iterable of metric class names or class identifiers, diff --git a/sklearn/metrics/pairwise.py b/sklearn/metrics/pairwise.py index f7d8e58b67dd2..0bd8dae42786c 100644 --- a/sklearn/metrics/pairwise.py +++ b/sklearn/metrics/pairwise.py @@ -1742,7 +1742,6 @@ def _pairwise_callable(X, Y, metric, force_all_finite=True, **kwds): "hamming", "jaccard", "mahalanobis", - "matching", "minkowski", "rogerstanimoto", "russellrao", @@ -1758,6 +1757,9 @@ def _pairwise_callable(X, Y, metric, force_all_finite=True, **kwds): if sp_base_version < parse_version("1.11"): # Deprecated in SciPy 1.9 and removed in SciPy 1.11 _VALID_METRICS += ["kulsinski"] +if sp_base_version < parse_version("1.9"): + # Deprecated in SciPy 1.0 and removed in SciPy 1.9 + _VALID_METRICS += ["matching"] _NAN_METRICS = ["nan_euclidean"] @@ -2150,7 +2152,6 @@ def pairwise_distances( PAIRWISE_BOOLEAN_FUNCTIONS = [ "dice", "jaccard", - "matching", "rogerstanimoto", "russellrao", "sokalmichener", @@ -2160,6 +2161,9 @@ def pairwise_distances( if sp_base_version < parse_version("1.11"): # Deprecated in SciPy 1.9 and removed in SciPy 1.11 PAIRWISE_BOOLEAN_FUNCTIONS += ["kulsinski"] +if sp_base_version < parse_version("1.9"): + # Deprecated in SciPy 1.0 and removed in SciPy 1.9 + PAIRWISE_BOOLEAN_FUNCTIONS += ["matching"] # Helper functions - distance PAIRWISE_KERNEL_FUNCTIONS = { diff --git a/sklearn/neighbors/_ball_tree.pyx b/sklearn/neighbors/_ball_tree.pyx index ef7033709d410..cf02bd37c5829 100644 --- a/sklearn/neighbors/_ball_tree.pyx +++ b/sklearn/neighbors/_ball_tree.pyx @@ -10,8 +10,7 @@ VALID_METRICS = ['EuclideanDistance', 'SEuclideanDistance', 'MinkowskiDistance', 'WMinkowskiDistance', 'MahalanobisDistance', 'HammingDistance', 'CanberraDistance', 'BrayCurtisDistance', - 'JaccardDistance', 'MatchingDistance', - 'DiceDistance', + 'JaccardDistance', 'DiceDistance', 'RogersTanimotoDistance', 'RussellRaoDistance', 'SokalMichenerDistance', 'SokalSneathDistance', 'PyFuncDistance', 'HaversineDistance'] diff --git a/sklearn/neighbors/_base.py b/sklearn/neighbors/_base.py index 9af85a38f0b6c..96c78da02059f 100644 --- a/sklearn/neighbors/_base.py +++ b/sklearn/neighbors/_base.py @@ -51,7 +51,6 @@ "hamming", "jaccard", "mahalanobis", - "matching", "minkowski", "rogerstanimoto", "russellrao", @@ -64,6 +63,9 @@ if sp_base_version < parse_version("1.11"): # Deprecated in SciPy 1.9 and removed in SciPy 1.11 SCIPY_METRICS += ["kulsinski"] +if sp_base_version < parse_version("1.9"): + # Deprecated in SciPy 1.0 and removed in SciPy 1.9 + SCIPY_METRICS += ["matching"] VALID_METRICS = dict( ball_tree=BallTree._valid_metrics, diff --git a/sklearn/neighbors/tests/test_ball_tree.py b/sklearn/neighbors/tests/test_ball_tree.py index 8d665f799e9d8..8fc1e58a0f40f 100644 --- a/sklearn/neighbors/tests/test_ball_tree.py +++ b/sklearn/neighbors/tests/test_ball_tree.py @@ -27,7 +27,6 @@ DISCRETE_METRICS = ["hamming", "canberra", "braycurtis"] BOOLEAN_METRICS = [ - "matching", "jaccard", "dice", "rogerstanimoto", From f71cadf8a87b35775e6bf5140512b89da0c8d43e Mon Sep 17 00:00:00 2001 From: Barata Onggo Date: Sun, 23 Apr 2023 23:31:56 +0900 Subject: [PATCH 2/5] Add fix info to whats_new --- doc/whats_new/v1.3.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats_new/v1.3.rst b/doc/whats_new/v1.3.rst index bb245aa466152..70777de7ba323 100644 --- a/doc/whats_new/v1.3.rst +++ b/doc/whats_new/v1.3.rst @@ -406,6 +406,9 @@ Changelog - |API| The `eps` parameter of the :func:`log_loss` has been deprecated and will be removed in 1.5. :pr:`25299` by :user:`Omar Salman `. +- |Fix| Deprecate `matching` as a metric to be consistent with `scipy.spatial.distance`. + :pr:`26264` by :user:`Barata T. Onggo ` + :mod:`sklearn.model_selection` .............................. From 0a2da508bd6ffbf69d5a1faac8601323347c5312 Mon Sep 17 00:00:00 2001 From: Barata Onggo Date: Tue, 9 May 2023 00:17:59 +0900 Subject: [PATCH 3/5] Add deprecation notes for `matching`. --- sklearn/metrics/pairwise.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sklearn/metrics/pairwise.py b/sklearn/metrics/pairwise.py index 1c6bfeec956d1..dae669af9ed9e 100644 --- a/sklearn/metrics/pairwise.py +++ b/sklearn/metrics/pairwise.py @@ -663,6 +663,9 @@ def pairwise_distances_argmin_min( .. note:: `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. + .. note:: + `'matching'` has been removed since SciPy 1.9 (use `'hamming'` instead). + metric_kwargs : dict, default=None Keyword arguments to pass to specified metric function. @@ -783,6 +786,9 @@ def pairwise_distances_argmin(X, Y, *, axis=1, metric="euclidean", metric_kwargs .. note:: `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. + .. note:: + `'matching'` has been removed since SciPy 1.9 (use `'hamming'` instead). + metric_kwargs : dict, default=None Keyword arguments to pass to specified metric function. @@ -2025,6 +2031,9 @@ def pairwise_distances( .. note:: `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. + .. note:: + `'matching'` has been removed since SciPy 1.9 (use `'hamming'` instead). + Note that in the case of 'cityblock', 'cosine' and 'euclidean' (which are valid scipy.spatial.distance metrics), the scikit-learn implementation will be used, which is faster and has support for sparse matrices (except From 462d96ab279d0453df9ab4da4e6d3fa2f22bbb3c Mon Sep 17 00:00:00 2001 From: Barata Tripramudya Onggo Date: Tue, 9 May 2023 11:00:31 +0900 Subject: [PATCH 4/5] Apply code review suggestions Change wordings for documentation Co-authored-by: Julien Jerphanion --- doc/whats_new/v1.3.rst | 4 +++- sklearn/metrics/pairwise.py | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/whats_new/v1.3.rst b/doc/whats_new/v1.3.rst index 2b0325ebff19e..eb9497c67e29a 100644 --- a/doc/whats_new/v1.3.rst +++ b/doc/whats_new/v1.3.rst @@ -416,7 +416,9 @@ Changelog `tpr=0` and `fpr=0`. :pr:`26194` by :user:`Guillaume Lemaitre `. -- |Fix| Deprecate `matching` as a metric to be consistent with `scipy.spatial.distance`. +- |Fix| `matching` has been deprecated as a metric when SciPy>=1.9 is used + to be consistent with `scipy.spatial.distance` which does not support it + anymore. :pr:`26264` by :user:`Barata T. Onggo ` :mod:`sklearn.model_selection` diff --git a/sklearn/metrics/pairwise.py b/sklearn/metrics/pairwise.py index dae669af9ed9e..b59d733f81723 100644 --- a/sklearn/metrics/pairwise.py +++ b/sklearn/metrics/pairwise.py @@ -664,7 +664,7 @@ def pairwise_distances_argmin_min( `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. .. note:: - `'matching'` has been removed since SciPy 1.9 (use `'hamming'` instead). + `'matching'` has been removed in SciPy 1.9 (use `'hamming'` instead). metric_kwargs : dict, default=None Keyword arguments to pass to specified metric function. @@ -787,7 +787,7 @@ def pairwise_distances_argmin(X, Y, *, axis=1, metric="euclidean", metric_kwargs `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. .. note:: - `'matching'` has been removed since SciPy 1.9 (use `'hamming'` instead). + `'matching'` has been removed in SciPy 1.9 (use `'hamming'` instead). metric_kwargs : dict, default=None Keyword arguments to pass to specified metric function. @@ -2032,7 +2032,7 @@ def pairwise_distances( `'kulsinski'` is deprecated from SciPy 1.9 and will be removed in SciPy 1.11. .. note:: - `'matching'` has been removed since SciPy 1.9 (use `'hamming'` instead). + `'matching'` has been removed in SciPy 1.9 (use `'hamming'` instead). Note that in the case of 'cityblock', 'cosine' and 'euclidean' (which are valid scipy.spatial.distance metrics), the scikit-learn implementation From 7156cbc8dbdb8061f020e40a7e58f670d68f70e9 Mon Sep 17 00:00:00 2001 From: Barata Tripramudya Onggo Date: Wed, 17 May 2023 22:55:56 +0900 Subject: [PATCH 5/5] Apply second reviewer code suggestion Co-authored-by: Thomas J. Fan --- doc/whats_new/v1.3.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/whats_new/v1.3.rst b/doc/whats_new/v1.3.rst index fa9b92b773ed8..6a4d9210376eb 100644 --- a/doc/whats_new/v1.3.rst +++ b/doc/whats_new/v1.3.rst @@ -420,9 +420,9 @@ Changelog `tpr=0` and `fpr=0`. :pr:`26194` by :user:`Guillaume Lemaitre `. -- |Fix| `matching` has been deprecated as a metric when SciPy>=1.9 is used - to be consistent with `scipy.spatial.distance` which does not support it - anymore. +- |Fix| The `'matching'` metric has been removed when using SciPy>=1.9 + to be consistent with `scipy.spatial.distance` which does not support + `'matching'` anymore. :pr:`26264` by :user:`Barata T. Onggo ` :mod:`sklearn.model_selection`