KNeighborsClassifier not accepting a DistanceMetric
See original GitHub issueUsing 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:
- Created 2 years ago
- Comments:14 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Valentin-Laurent, sure go ahead.
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.