8000 KNeighborsClassifier not accepting a DistanceMetric · Issue #22348 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

KNeighborsClassifier not accepting a DistanceMetric #22348

@adrinjalali

Description

@adrinjalali

Using a DistanceMetric metric in KNeighborsClassifier raises. Our documentation of the class says:

DistanceMetric class

This class provides a uniform interface to fast distance metric functions. The various metrics can be accessed via the get_metric class method and the metric string identifier (see below).

From that documentation, I would expect the following code to work. I'm not sure if we want to fix the documentation or the code.

from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import DistanceMetric as dm
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
clf = KNeighborsClassifier(metric="euclidean")
clf.fit(X,y)
clf = KNeighborsClassifier(metric=dm.get_metric("euclidean"))
clf.fit(X,y)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Amanda\Miniconda3\envs\mlenv\lib\site-packages\sklearn\neighbors\_classification.py", line 198, in fit
    return self._fit(X, y)
  File "C:\Users\Amanda\Miniconda3\envs\mlenv\lib\site-packages\sklearn\neighbors\_base.py", line 437, in _fit
    self._check_algorithm_metric()
  File "C:\Users\Amanda\Miniconda3\envs\mlenv\lib\site-packages\sklearn\neighbors\_base.py", line 374, in _check_algorithm_metric
    raise ValueError(
ValueError: Metric '<sklearn.metrics._dist_metrics.EuclideanDistance object at 0x0000018099BB9780>' not valid. Use sorted(sklearn.neighbors.VALID_METRICS['brute']) to get valid options. Metric can also be a callable function.

Pinging @jeremiedbb @lorentzenchr and maybe @ogrisel

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0