英文:
How to build a BallTree with haversine distance metric?
问题
我一直在研究如何使用sklearn.metrics.pairwise.haversine_distances
度量来实现sklearn.neighbors.BallTree
。尽管我付出了努力,但我无法得到一个可用的脚本。尽管在sklearn文档中有一个标准示例这里,但当尝试在BallTree中使用haversine距离度量时,整个类的初始化都会导致ValueError。请看下面的简单脚本,它会导致此问题:
from sklearn.neighbors import BallTree
import numpy as np
from sklearn import metrics
X = rng.random_sample((10, 2)) # 2维空间中的10个点
tree = BallTree(X, metric=metrics.pairwise.haversine_distances)
返回的错误信息:
ValueError: Buffer has wrong number of dimensions (expected 2, got 1)
如何解决这个问题?
英文:
I have been studying how to implement a sklearn.neighbors.BallTree
with sklearn.metrics.pairwise.haversine_distances
metric.
Despite my efforts, I couldn't reach a working script.
Despite the standard example from the sklearn documentation here, when one attempts to use the haversine distance metric within the BallTree, the whole initialization of the class breaks into a ValueError. See below a simple script that results in this problem:
from sklearn.neighbors import BallTree
import numpy as np
from sklearn import metrics
X = rng.random_sample((10, 2)) # 10 points in 2 dimensions
tree = BallTree(X, metric=metrics.pairwise.haversine_distances)
Returned error:
ValueError: Buffer has wrong number of dimensions (expected 2, got 1)
How to resolve this?
答案1
得分: 2
使用 metric="haversine"
。从文档(原文中的重点):
> 注意:不支持在KDTree和Ball Tree的metric参数中使用可调用函数。函数调用开销会导致性能非常差。
另请参阅distance_metrics上的文档。
英文:
Use metric="haversine"
. From the docs (emphasis in the original):
> Note: Callable functions in the metric parameter are NOT supported for KDTree
and Ball Tree. Function call overhead will result in very poor performance.
See also the documentation on distance_metrics.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论