如何使用haversine距离度量构建BallTree?

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 22:09:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定