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.

KDTree should support Haversine Metric

See original GitHub issue

Related to #12552

Using the Haversine metric with the KDTree produces an error

haversineMetric = DistanceMetric.get_metric('haversine')
coordsKdTree = KDTree(coords, leaf_size =1e3, metric = haversineMetric)

ValueError                                Traceback (most recent call last)
<ipython-input-15-fa4421f977f6> in <module>
     10 #Create KD tree with haversine metric
     11 haversineMetric = DistanceMetric.get_metric('haversine')
---> 12 coordsKdTree = KDTree(coords, leaf_size =1e3, metric = haversineMetric)

sklearn/neighbors/_binary_tree.pxi in sklearn.neighbors._kd_tree.BinaryTree.__init__()

ValueError: metric HaversineDistance is not valid for KDTree

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rthcommented, Jul 28, 2021

Though it was added to Ball Tree by Jake at the time of the original implementation (https://github.com/scikit-learn/scikit-learn/commit/01fce7661632c7898feaf9a2e1dc3ce97a91473d, https://github.com/scikit-learn/scikit-learn/pull/1732) and none of the reviewers disagreed. So maybe we are missing something. I guess we could always try to find an counter-example to the triangle inequality with Haversine distance to be sure 😃

0reactions
rthcommented, Sep 13, 2022

My math on the sphere is pretty bad, and it sounds like you are right 😃

there are other reasons, why the haversine metric should be used with caution with KD-Trees:

The k-d-tree can (to the best of my knowledge)

Thanks for investigating! Probably safter to keep this closed then (though the latter part needs better citations ^^)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

python sklearn KDTree with haversine distance - Stack Overflow
Which tells, you can't use haversine with KDTree. The reason behind it is haversine distance gives you Orthodromic distance which is the ...
Read more >
Using Scikit-learn's Binary Trees to Efficiently Find Latitude ...
To use a ball tree with the Haversine distance in scikit-learn, you must first convert the coordinates from degrees to radians. # Creates...
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 >
Nearest neighbor analysis with large datasets - Read the Docs
Note: There is also an algorithm called KDTree in scikit-learn, that is also highly efficient but less flexible in terms of supported distance...
Read more >
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 >

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