8000 ENH Better error message for sparse metrics of neighbors (#12073) · scikit-learn/scikit-learn@1049fb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1049fb1

Browse files
zjpohqinhanmin2014
authored andcommitted
ENH Better error message for sparse metrics of neighbors (#12073)
1 parent 781edc4 commit 1049fb1

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

sklearn/neighbors/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .kde import KernelDensity
1515
from .approximate import LSHForest
1616
from .lof import LocalOutlierFactor
17-
from .base import VALID_METRICS
17+
from .base import VALID_METRICS, VALID_METRICS_SPARSE
1818

1919
__all__ = ['BallTree',
2020
'DistanceMetric',
@@ -30,4 +30,5 @@
3030
'KernelDensity',
3131
'LSHForest',
3232
'LocalOutlierFactor',
33-
'VALID_METRICS']
33+
'VALID_METRICS',
34+
'VALID_METRICS_SPARSE']

sklearn/neighbors/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,12 @@ def _fit(self, X):
217217
"using brute force")
218218
if self.effective_metric_ not in VALID_METRICS_SPARSE['brute'] \
219219
and not callable(self.effective_metric_):
220-
221-
raise ValueError("metric '%s' not valid for sparse input"
222-
% self.effective_metric_)
220+
raise ValueError("Metric '%s' not valid for sparse input. "
221+
"Use sorted(sklearn.neighbors."
222+
"VALID_METRICS_SPARSE['brute']) "
223+
"to get valid options. "
224+
"Metric can also be a callable function."
225+
% (self.effective_metric_))
223226
self._fit_X = X.copy()
224227
self._tree = None
225228
self._fit_method = 'brute'

0 commit comments

Comments
 (0)
0