question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KNeighborsClassifier not accepting a DistanceMetric

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
hassanrahim26commented, May 24, 2022

@Valentin-Laurent, sure go ahead.

1reaction
amy12xxcommented, Feb 1, 2022

Just to add some context, I raised this issue (in Gitter) when I was trying to pass different metrics to the validation_curve function for comparing KNN performance, but with non default parameters (different norms). So I could not directly use the string. But I got around this issue, by varying the parameter “p” instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sklearn kNN usage with a user defined metric - Stack Overflow
im using python, sklearn package to do the job, but our predefined metric is not one of those default metrics. so I have...
Read more >
K Nearest Neighbours - accuracy not changing with ...
From here, I attempted changing KNeighborsClassifier parameters to increase the accuracy for both testing and training set, however, ...
Read more >
1.6. Nearest Neighbors — scikit-learn 1.2.0 documentation
Neighbors-based methods are known as non-generalizing machine learning methods, ... nearest neighbors classifiers: KNeighborsClassifier implements learning ...
Read more >
Most Popular Distance Metrics Used in KNN and When to Use ...
For calculating distances KNN uses a distance metric from the list of available metrics. Read this article for an overview of these metrics, ......
Read more >
k-nearest neighbor classification - MATLAB - MathWorks
ClassificationKNN is a nearest-neighbor classification model in which you can alter both the distance metric and the number of nearest neighbors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found