KD树与暴力方法产生不同的结果

2024-04-19 19:01:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个1000个随机三维点的数组,我对离任何给定点最近的10个点感兴趣。In essence the same as this post.

我检查了J.F.Sebastian提供的两种解决方案,即蛮力方法和KD树方法。在

虽然两种方法给出的最接近点的指数相同,但它们给出的距离结果却不同

import numpy as np
from scipy.spatial import KDTree

a = 100 * np.random.rand(1000,3)
point = a[np.random.randint(0, 1001)] # point chosen at random

# KD Tree
tree = KDTree(a, leafsize=a.shape[0]+1)
dist_kd, ndx_kd = tree.query([point], k=10)

# Brute force
distances = ((a-point)**2).sum(axis=1)  # compute distances
ndx = distances.argsort() # indirect sort 
ndx_brt = ndx[:10]
dist_brt = distances[ndx[:10]]

# Output
print 'KD Tree:'
print ndx_kd
print dist_kd
print
print 'Brute force:'
print ndx_brt
print dist_brt

我的输出

KD Tree: [[838 860 595 684 554 396 793 197 652 330]] [[ 0. 3.00931208 8.30596471 9.47709122 10.98784209 11.39555636 11.89088764 12.01566931 12.551557 12.77700426]]

Brute force: [838 860 595 684 554 396 793 197 652 330]
[ 0. 9.05595922 68.9890498 89.81525793 120.73267386 129.8587047 141.3932089 144.37630888 157.54158301 163.25183793]

那么这里的问题是什么?我算错了距离吗?在


Tags: 方法treedistasnprandomkdpoint
2条回答

KDTree算法是根据Brute-force算法使用的相同距离的平方根来计算最近点。在

基本上KDtree使用:sqrt(x^2+y^2+z^2) 暴力算法使用:x^2+y^2+z^2

距离=((a点)**2).和(轴=1)**0.5enter image description here

相关问题 更多 >