我有一个1000个随机3D点阵列&我对任何给定点最近的10分感兴趣。 In essence the same as this post.
我检查了J.F.Sebastian提供的2种解决方案,即蛮力方法& 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树: [[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]]
蛮力: [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]
那么这里的问题是什么?我计算距离错了吗?