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.

Add callable metric support for KDTree algorithm

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Comments:18 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
adrinjalalicommented, May 27, 2020

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.

1reaction
VolodyaCOcommented, May 26, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

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