Add callable metric support for KDTree algorithm
See original GitHub issueDescription
Related Issue: https://github.com/scikit-learn/scikit-learn/issues/10386 Currently, a callable metric for the KDTree is not supported. For example, when using NearestNeighbors as an interface for neighbors algorithm:
Steps/Code to Reproduce
def heom_metric(x, y):
# metric function
# E.g Heterogeneous Euclidian-Overlap Metric
pass
nbrs_kd = NearestNeighbors(algorithm='kd_tree',
metric=heom_metric)
nbrs_ball = NearestNeighbors(algorithm='ball_tree',
metric=heom_metric)
Expected Results
Code runs correctly without any ValueError thrown
Actual Results
nbrs_kd will throw a ValueError that a callable metric is not supported. It would be helpful to make the API consistent and add callable metric support to KDTree as well. At the moment it is a bit misleading as metrics are not consistent among different Nearest Neighbors Algorithms.
Issue Analytics
- State:
- Created 4 years ago
- Comments:18 (13 by maintainers)
Top Results From Across the Web
sklearn.neighbors.KDTree — scikit-learn 1.2.0 documentation
Note: Callable functions in the metric parameter are NOT supported for KDTree: and Ball Tree. Function call overhead will result in very poor...
Read more >'KD tree' with custom distance metric - Stack Overflow
Scikit-learn's KDTree does not support custom distance metrics. The BallTree does support custom distance metrics, but be careful: it is up ...
Read more >Benchmarking Nearest Neighbor Searches in Python
I recently submitted a scikit-learn pull request containing a brand new ball tree and kd-tree for fast nearest neighbor searches in python.
Read more >[Example code]-'KD tree' with custom distance metric
Scikit-learn's KDTree does not support custom distance metrics. The BallTree does support custom distance metrics, but be careful: it is up to the...
Read more >python - Find K-nearest neighbour with custom distance metric
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should...
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 Free
Top 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

That’ll add quite a bit of complexity to the existing code. If somebody ends up submittig a PR which looks like nice maintainable code and doesn’t add a time penalty to the existing one, I’m happy to review.
There are some applications where a custom metric is needed (even if it is slower). I recently wanted to use cKDTree (with periodic boundary conditions) with a custom metric for molecular dynamics purposes, but it was impossible. @amueller will this feature request be reconsidered?