From 5b094c6adac1c40b88279ef673ac1e0c19d72fcc Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Wed, 25 Jan 2023 18:43:08 -0500 Subject: [PATCH 01/11] ENH: Update KDTree, and example documentation --- doc/modules/neighbors.rst | 4 +--- sklearn/neighbors/_binary_tree.pxi | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index dfd6791d9a3d3..149597a09d4d4 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -136,9 +136,7 @@ have the same interface; we'll show an example of using the KD Tree here: Refer to the :class:`KDTree` and :class:`BallTree` class documentation for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list -of available metrics, see the documentation of the :class:`DistanceMetric` class -and the metrics listed in `sklearn.metrics.pairwise.PAIRWISE_DISTANCE_FUNCTIONS`. -Note that the "cosine" metric uses :func:`~sklearn.metrics.pairwise.cosine_distances`. +of valid metrics use {BallTree,KDTree}.valid_metrics. .. _classification: diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 00b5b3c2758d3..1f50dbb3aa268 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -234,7 +234,7 @@ leaf_size : positive int, default=40 metric : str or DistanceMetric object, default='minkowski' Metric to use for distance computation. Default is "minkowski", which results in the standard Euclidean distance when p = 2. - {binary_tree}.valid_metrics gives a list of the metrics which are valid for + {BinaryTree}.valid_metrics gives a list of the metrics which are valid for {BinaryTree}. See the documentation of `scipy.spatial.distance `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for From 4b046162d442a54be384100ed51dc9a421eaf4e6 Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Mon, 30 Jan 2023 16:01:43 -0500 Subject: [PATCH 02/11] ENH: Add valid metric function and reference doc --- doc/modules/neighbors.rst | 2 +- sklearn/neighbors/_binary_tree.pxi | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 149597a09d4d4..cd5dd3b102be1 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -136,7 +136,7 @@ have the same interface; we'll show an example of using the KD Tree here: Refer to the :class:`KDTree` and :class:`BallTree` class documentation for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list -of valid metrics use {BallTree,KDTree}.valid_metrics. +of valid metrics use {BallTree,KDTree}.get_valid_metric. .. _classification: diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 1f50dbb3aa268..fb33377937e9d 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -233,9 +233,8 @@ leaf_size : positive int, default=40 metric : str or DistanceMetric object, default='minkowski' Metric to use for distance computation. Default is "minkowski", which - results in the standard Euclidean distance when p = 2. - {BinaryTree}.valid_metrics gives a list of the metrics which are valid for - {BinaryTree}. See the documentation of `scipy.spatial.distance + results in the standard Euclidean distance when p = 2. A list of valid metrics for {BinaryTree} is given by + :class:`~sklearn.neighbors.{BinaryTree}.get_valid_metrics`. See the documentation of `scipy.spatial.distance `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for more information. @@ -979,6 +978,20 @@ cdef class BinaryTree: self.node_bounds.base, ) + def get_valid_metrics(self): + """ + get_valid_metrics() + + Get valid metrics. + + Returns + ------- + valid_metrics: list of str + List of valid metrics. + """ + return self.valid_metrics + + cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, ITYPE_t size) nogil except -1: """Compute the distance between arrays x1 and x2""" From 5356ebe8e25196c8e10a79f346102edd968b9218 Mon Sep 17 00:00:00 2001 From: Vyom Pathak Date: Mon, 30 Jan 2023 19:15:00 -0500 Subject: [PATCH 03/11] CHG: Documentation update Co-authored-by: Adam Li --- sklearn/neighbors/_binary_tree.pxi | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index fb33377937e9d..27da11a752719 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -979,10 +979,7 @@ cdef class BinaryTree: ) def get_valid_metrics(self): - """ - get_valid_metrics() - - Get valid metrics. + """Get list of valid distance metrics. Returns ------- From 898e37b0b36c1a8c9d8dfb7a455c892e419f0a8a Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Fri, 3 Feb 2023 17:32:59 -0500 Subject: [PATCH 04/11] CHG: make valid metric property and fix doc string --- doc/modules/neighbors.rst | 2 +- sklearn/neighbors/_binary_tree.pxi | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index cd5dd3b102be1..a01bde7d22f9f 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -136,7 +136,7 @@ have the same interface; we'll show an example of using the KD Tree here: Refer to the :class:`KDTree` and :class:`BallTree` class documentation for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list -of valid metrics use {BallTree,KDTree}.get_valid_metric. +of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric`. .. _classification: diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 27da11a752719..1ad9a0c98d6b4 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -234,10 +234,10 @@ leaf_size : positive int, default=40 metric : str or DistanceMetric object, default='minkowski' Metric to use for distance computation. Default is "minkowski", which results in the standard Euclidean distance when p = 2. A list of valid metrics for {BinaryTree} is given by - :class:`~sklearn.neighbors.{BinaryTree}.get_valid_metrics`. See the documentation of `scipy.spatial.distance + :meth:`{BinaryTree}.valid_metric`. See the documentation of `scipy.spatial.distance `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for - more information. + more information on any individual metric. Additional keywords are passed to the distance metric class. Note: Callable functions in the metric parameter are NOT supported for KDTree @@ -978,12 +978,13 @@ cdef class BinaryTree: self.node_bounds.base, ) - def get_valid_metrics(self): + @property + def valid_metric(self): """Get list of valid distance metrics. Returns ------- - valid_metrics: list of str + valid_metric: list of str List of valid metrics. """ return self.valid_metrics From 2890086262eb68516e6856bb0bb4c353efbaaf70 Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Wed, 8 Feb 2023 22:12:37 -0500 Subject: [PATCH 05/11] FIX: documentation, and add code example --- doc/modules/neighbors.rst | 14 +++++++++++++- sklearn/neighbors/_binary_tree.pxi | 7 ++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index a01bde7d22f9f..629fb36e0af32 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -136,7 +136,19 @@ have the same interface; we'll show an example of using the KD Tree here: Refer to the :class:`KDTree` and :class:`BallTree` class documentation for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list -of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric`. +of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric` as shown here: + + >>> from sklearn.neighbors import KDTree, BallTree + >>> dummy_X = np.zeros((1, 1)) + >>> kdt = KDTree(dummy_X) + >>> bt = BallTree(dummy_X) + >>> print(f'Valid list of KDTree metrics:\n {kdt.valid_metric}') + ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity'] + >>> print(f'Valid list of BallTree metrics:\n {bt.valid_metric}') + ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', + 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', + 'jaccard', 'dice', 'kulsinski', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', + 'haversine', 'pyfunc'] .. _classification: diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 1ad9a0c98d6b4..9fc725592bfd5 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -237,7 +237,7 @@ metric : str or DistanceMetric object, default='minkowski' :meth:`{BinaryTree}.valid_metric`. See the documentation of `scipy.spatial.distance `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for - more information on any individual metric. + more information on any distance metric. Additional keywords are passed to the distance metric class. Note: Callable functions in the metric parameter are NOT supported for KDTree @@ -982,14 +982,15 @@ cdef class BinaryTree: def valid_metric(self): """Get list of valid distance metrics. + .. versionadded:: 1.3 + Returns ------- valid_metric: list of str - List of valid metrics. + List of valid distance metrics. """ return self.valid_metrics - cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, ITYPE_t size) nogil except -1: """Compute the distance between arrays x1 and x2""" From a7a2ea5fe17e51ec04b66c51f8a24456b0d223a7 Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Thu, 9 Feb 2023 17:46:43 -0500 Subject: [PATCH 06/11] ENH: Change valid metric to class method, and doc --- doc/modules/neighbors.rst | 11 +++-------- sklearn/neighbors/_binary_tree.pxi | 13 +++++++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 629fb36e0af32..82c9c28e4db03 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -136,20 +136,15 @@ have the same interface; we'll show an example of using the KD Tree here: Refer to the :class:`KDTree` and :class:`BallTree` class documentation for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list -of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric` as shown here: - +of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric`: >>> from sklearn.neighbors import KDTree, BallTree - >>> dummy_X = np.zeros((1, 1)) - >>> kdt = KDTree(dummy_X) - >>> bt = BallTree(dummy_X) - >>> print(f'Valid list of KDTree metrics:\n {kdt.valid_metric}') + >>> KDTree.valid_metric()) ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity'] - >>> print(f'Valid list of BallTree metrics:\n {bt.valid_metric}') + >>> BallTree.valid_metric()) ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', 'jaccard', 'dice', 'kulsinski', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', 'haversine', 'pyfunc'] - .. _classification: Nearest Neighbors Classification diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 9fc725592bfd5..a904441d7d37e 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -233,9 +233,10 @@ leaf_size : positive int, default=40 metric : str or DistanceMetric object, default='minkowski' Metric to use for distance computation. Default is "minkowski", which - results in the standard Euclidean distance when p = 2. A list of valid metrics for {BinaryTree} is given by - :meth:`{BinaryTree}.valid_metric`. See the documentation of `scipy.spatial.distance - `_ and the + results in the standard Euclidean distance when p = 2. + A list of valid metrics for {BinaryTree} is given by + :meth:`{BinaryTree}.valid_metric`. + See the documentation of `scipy.spatial.distance `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for more information on any distance metric. @@ -978,8 +979,8 @@ cdef class BinaryTree: self.node_bounds.base, ) - @property - def valid_metric(self): + @classmethod + def valid_metric(cls): """Get list of valid distance metrics. .. versionadded:: 1.3 @@ -989,7 +990,7 @@ cdef class BinaryTree: valid_metric: list of str List of valid distance metrics. """ - return self.valid_metrics + return cls.valid_metrics cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, ITYPE_t size) nogil except -1: From 7d04662abec77d1690bb1b031b9c2ca02f69bed9 Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Fri, 10 Feb 2023 15:40:08 -0500 Subject: [PATCH 07/11] ENH: Change valid metric class variable, and doc --- doc/modules/neighbors.rst | 6 +++--- sklearn/neighbors/_base.py | 4 ++-- sklearn/neighbors/_binary_tree.pxi | 10 +++++----- sklearn/neighbors/_kde.py | 6 +++--- sklearn/neighbors/tests/test_kde.py | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 82c9c28e4db03..0d655abb4ee3a 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -138,9 +138,9 @@ for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric`: >>> from sklearn.neighbors import KDTree, BallTree - >>> KDTree.valid_metric()) + >>> KDTree.valid_metrics()) ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity'] - >>> BallTree.valid_metric()) + >>> BallTree.valid_metrics()) ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', 'jaccard', 'dice', 'kulsinski', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', @@ -481,7 +481,7 @@ A list of valid metrics for any of the above algorithms can be obtained by using ``valid_metric`` attribute. For example, valid metrics for ``KDTree`` can be generated by: >>> from sklearn.neighbors import KDTree - >>> print(sorted(KDTree.valid_metrics)) + >>> print(sorted(KDTree.valid_metrics())) ['chebyshev', 'cityblock', 'euclidean', 'infinity', 'l1', 'l2', 'manhattan', 'minkowski', 'p'] diff --git a/sklearn/neighbors/_base.py b/sklearn/neighbors/_base.py index 35bfbc00137e4..88febbd9a3aea 100644 --- a/sklearn/neighbors/_base.py +++ b/sklearn/neighbors/_base.py @@ -66,8 +66,8 @@ SCIPY_METRICS += ["kulsinski"] VALID_METRICS = dict( - ball_tree=BallTree.valid_metrics, - kd_tree=KDTree.valid_metrics, + ball_tree=BallTree._valid_metrics, + kd_tree=KDTree._valid_metrics, # The following list comes from the # sklearn.metrics.pairwise doc string brute=sorted(set(PAIRWISE_DISTANCE_FUNCTIONS).union(SCIPY_METRICS)), diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index a904441d7d37e..42d66bffde5a2 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -235,7 +235,7 @@ metric : str or DistanceMetric object, default='minkowski' Metric to use for distance computation. Default is "minkowski", which results in the standard Euclidean distance when p = 2. A list of valid metrics for {BinaryTree} is given by - :meth:`{BinaryTree}.valid_metric`. + :meth:`{BinaryTree}.valid_metrics`. See the documentation of `scipy.spatial.distance `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for more information on any distance metric. @@ -791,7 +791,7 @@ cdef class BinaryTree: cdef int n_splits cdef int n_calls - valid_metrics = VALID_METRIC_IDS + _valid_metrics = VALID_METRIC_IDS # Use cinit to initialize all arrays to empty: this will prevent memory # errors and seg-faults in rare cases where __init__ is not called @@ -980,17 +980,17 @@ cdef class BinaryTree: ) @classmethod - def valid_metric(cls): + def valid_metrics(cls): """Get list of valid distance metrics. .. versionadded:: 1.3 Returns ------- - valid_metric: list of str + valid_metrics: list of str List of valid distance metrics. """ - return cls.valid_metrics + return cls._valid_metrics cdef inline DTYPE_t dist(self, DTYPE_t* x1, DTYPE_t* x2, ITYPE_t size) nogil except -1: diff --git a/sklearn/neighbors/_kde.py b/sklearn/neighbors/_kde.py index d7ffed501b1ae..52a2a78165752 100644 --- a/sklearn/neighbors/_kde.py +++ b/sklearn/neighbors/_kde.py @@ -174,12 +174,12 @@ def _choose_algorithm(self, algorithm, metric): # algorithm to compute the result. if algorithm == "auto": # use KD Tree if possible - if metric in KDTree.valid_metrics: + if metric in KDTree._valid_metrics: return "kd_tree" - elif metric in BallTree.valid_metrics: + elif metric in BallTree._valid_metrics: return "ball_tree" else: # kd_tree or ball_tree - if metric not in TREE_DICT[algorithm].valid_metrics: + if metric not in TREE_DICT[algorithm]._valid_metrics: raise ValueError( "invalid metric for {0}: '{1}'".format(TREE_DICT[algorithm], metric) ) diff --git a/sklearn/neighbors/tests/test_kde.py b/sklearn/neighbors/tests/test_kde.py index 23fa12a3c3a56..ec954b10d8780 100644 --- a/sklearn/neighbors/tests/test_kde.py +++ b/sklearn/neighbors/tests/test_kde.py @@ -114,7 +114,7 @@ def test_kde_algorithm_metric_choice(algorithm, metric): kde = KernelDensity(algorithm=algorithm, metric=metric) - if algorithm == "kd_tree" and metric not in KDTree.valid_metrics: + if algorithm == "kd_tree" and metric not in KDTree._valid_metrics: with pytest.raises(ValueError, match="invalid metric"): kde.fit(X) else: @@ -165,7 +165,7 @@ def test_kde_sample_weights(): test_points = rng.rand(n_samples_test, d) for algorithm in ["auto", "ball_tree", "kd_tree"]: for metric in ["euclidean", "minkowski", "manhattan", "chebyshev"]: - if algorithm != "kd_tree" or metric in KDTree.valid_metrics: + if algorithm != "kd_tree" or metric in KDTree._valid_metrics: kde = KernelDensity(algorithm=algorithm, metric=metric) # Test that adding a constant sample weight has no effect From cbdd2caca4c3c979a94d3a433784982adb892e27 Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Fri, 10 Feb 2023 21:49:41 -0500 Subject: [PATCH 08/11] FIX: documentation error --- doc/modules/neighbors.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 0d655abb4ee3a..2dffbdde8adde 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -136,7 +136,8 @@ have the same interface; we'll show an example of using the KD Tree here: Refer to the :class:`KDTree` and :class:`BallTree` class documentation for more information on the options available for nearest neighbors searches, including specification of query strategies, distance metrics, etc. For a list -of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metric`: +of valid metrics use :meth:`KDTree.valid_metrics` and :meth:`BallTree.valid_metrics`: + >>> from sklearn.neighbors import KDTree, BallTree >>> KDTree.valid_metrics()) ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity'] @@ -145,6 +146,7 @@ of valid metrics use :meth:`KDTree.valid_metric` and :meth:`BallTree.valid_metri 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', 'jaccard', 'dice', 'kulsinski', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', 'haversine', 'pyfunc'] + .. _classification: Nearest Neighbors Classification From 7981c5a27588c3ec171cb00be6d1422e8f095572 Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Sat, 11 Feb 2023 11:44:18 -0500 Subject: [PATCH 09/11] FIX: documentation error --- doc/modules/neighbors.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 2dffbdde8adde..075c8bc3dcd0f 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -139,9 +139,9 @@ including specification of query strategies, distance metrics, etc. For a list of valid metrics use :meth:`KDTree.valid_metrics` and :meth:`BallTree.valid_metrics`: >>> from sklearn.neighbors import KDTree, BallTree - >>> KDTree.valid_metrics()) + >>> KDTree.valid_metrics() ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity'] - >>> BallTree.valid_metrics()) + >>> BallTree.valid_metrics() ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', 'jaccard', 'dice', 'kulsinski', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', From 373f5e77b1b6ef40ddf3d8503b9bad2e0f253b9b Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Tue, 14 Feb 2023 12:55:16 -0500 Subject: [PATCH 10/11] CHG: Use class method for valid metrics --- sklearn/neighbors/_binary_tree.pxi | 4 ++-- sklearn/neighbors/_kde.py | 6 +++--- sklearn/neighbors/tests/test_kde.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sklearn/neighbors/_binary_tree.pxi b/sklearn/neighbors/_binary_tree.pxi index 42d66bffde5a2..1251932ab73f9 100644 --- a/sklearn/neighbors/_binary_tree.pxi +++ b/sklearn/neighbors/_binary_tree.pxi @@ -236,8 +236,8 @@ metric : str or DistanceMetric object, default='minkowski' results in the standard Euclidean distance when p = 2. A list of valid metrics for {BinaryTree} is given by :meth:`{BinaryTree}.valid_metrics`. - See the documentation of `scipy.spatial.distance `_ and the - metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for + See the documentation of `scipy.spatial.distance + `_ and the metrics listed in :class:`~sklearn.metrics.pairwise.distance_metrics` for more information on any distance metric. Additional keywords are passed to the distance metric class. diff --git a/sklearn/neighbors/_kde.py b/sklearn/neighbors/_kde.py index 52a2a78165752..8aa6e8c8ffc0d 100644 --- a/sklearn/neighbors/_kde.py +++ b/sklearn/neighbors/_kde.py @@ -174,12 +174,12 @@ def _choose_algorithm(self, algorithm, metric): # algorithm to compute the result. if algorithm == "auto": # use KD Tree if possible - if metric in KDTree._valid_metrics: + if metric in KDTree.valid_metrics(): return "kd_tree" - elif metric in BallTree._valid_metrics: + elif metric in BallTree.valid_metrics(): return "ball_tree" else: # kd_tree or ball_tree - if metric not in TREE_DICT[algorithm]._valid_metrics: + if metric not in TREE_DICT[algorithm].valid_metrics(): raise ValueError( "invalid metric for {0}: '{1}'".format(TREE_DICT[algorithm], metric) ) diff --git a/sklearn/neighbors/tests/test_kde.py b/sklearn/neighbors/tests/test_kde.py index ec954b10d8780..69cd3c8f5693f 100644 --- a/sklearn/neighbors/tests/test_kde.py +++ b/sklearn/neighbors/tests/test_kde.py @@ -114,7 +114,7 @@ def test_kde_algorithm_metric_choice(algorithm, metric): kde = KernelDensity(algorithm=algorithm, metric=metric) - if algorithm == "kd_tree" and metric not in KDTree._valid_metrics: + if algorithm == "kd_tree" and metric not in KDTree.valid_metrics(): with pytest.raises(ValueError, match="invalid metric"): kde.fit(X) else: @@ -165,7 +165,7 @@ def test_kde_sample_weights(): test_points = rng.rand(n_samples_test, d) for algorithm in ["auto", "ball_tree", "kd_tree"]: for metric in ["euclidean", "minkowski", "manhattan", "chebyshev"]: - if algorithm != "kd_tree" or metric in KDTree._valid_metrics: + if algorithm != "kd_tree" or metric in KDTree.valid_metrics(): kde = KernelDensity(algorithm=algorithm, metric=metric) # Test that adding a constant sample weight has no effect From cc02a048c5ab00ea7d849e086a5e73ba710adc2e Mon Sep 17 00:00:00 2001 From: 01-vyom Date: Wed, 15 Feb 2023 11:13:48 -0500 Subject: [PATCH 11/11] FIX: CI problems --- doc/modules/neighbors.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/doc/modules/neighbors.rst b/doc/modules/neighbors.rst index 075c8bc3dcd0f..7112c2a697651 100644 --- a/doc/modules/neighbors.rst +++ b/doc/modules/neighbors.rst @@ -142,10 +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', 'kulsinski', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', - 'haversine', 'pyfunc'] + ['euclidean', 'l2', 'minkowski', 'p', 'manhattan', 'cityblock', 'l1', 'chebyshev', 'infinity', 'seuclidean', 'mahalanobis', 'wminkowski', 'hamming', 'canberra', 'braycurtis', 'matching', 'jaccard', 'dice', 'rogerstanimoto', 'russellrao', 'sokalmichener', 'sokalsneath', 'haversine', 'pyfunc'] .. _classification: