问题描述
我正在尝试对 Lat 和 Lon 数据实施最近邻搜索.这是Data.txt
I am trying to implement a Nearest neighbour search for Lat and Lon data. Here is the Data.txt
61.3000183105 -21.2500038147 0
62.299987793 -23.750005722 1
66.3000488281 -28.7500038147 2
40.8000183105 -18.250005722 3
71.8000183105 -35.7500038147 3
39.3000183105 -19.7500019073 4
39.8000183105 -20.7500038147 5
41.3000183105 -20.7500038147 6
问题是,当我想为数据集上的每个纬度和经度做最近的邻居时,它正在自行搜索.例如 (-21.2500038147,61.3000183105) 的最近邻居将是 (-21.2500038147,61.3000183105) 并且由此产生的距离将是 0.0.我试图避免这种情况,但没有运气.我尝试过 if not (array_equal) 但仍然...
The problem is, when I want to do the nearest neighbour for each of the Lat and Lon on the data set, it is searching it self. e.g Nearest Neighbour of (-21.2500038147,61.3000183105) will be (-21.2500038147,61.3000183105) and the resulting distance will be 0.0. I am trying to avoid this but with no luck. I tried doing if not (array_equal) but still...
下面是我的python代码
Below is my python code
import numpy as np
from numpy import *
import decimal
from scipy import spatial
from scipy.spatial import KDTree
from math import radians,cos,sin,sqrt,exp
Lat =[]
Lon =[]
Day =[]
nja = []
Data = np.loadtxt('Data.txt',delimiter=" ")
for i in range(0,len(Data)):
Lon.append(Data[i][:][0])
Lat.append(Data[i][:][1])
Day.append(Data[i][:][2])
tree =spatial.KDTree(zip(Lon,Lat) )
print "Lon :",len(Lon)
print "Tree :",len(tree.data)
for i in range(0,len(tree.data)):
pts = np.array([tree.data[i][0],tree.data[i][1]])
nja.append(pts)
for i in range(0, len(nja)):
if not (np.array_equal(nja,tree.data)):
nearest = tree.query(pts,k=1,distance_upper_bound =9)
print nearest
推荐答案
对于数据集中的每个点 P[i]
,您都在问哪个点离 最近P[i]
在我的数据集中?"你会得到答案It is P[i]
".
For each point P[i]
in your data set, you're asking "Which is the point nearest to P[i]
in my data set?" and you get the answer "It is P[i]
".
如果你问一个不同的问题,哪两个点最接近 P[i]
?",即 tree.query(pts,k=2)
(与您的代码的区别是 s/k=1/k=2/
)你会得到 P[i]
和 P[j]
,第二个最近的点,这就是你想要的结果.
If you ask a different question, "Which are the TWO points nearest to P[i]
?", i.e., tree.query(pts,k=2)
(the difference with your code being s/k=1/k=2/
)you will get P[i]
and also a P[j]
, the second nearest point, that is the result you want.
附注:
- 我建议您在构建树之前投影数据,因为在您的纬度范围内,经度 1 度距离的含义存在很大波动.
这篇关于距离大于零的 Python KD 树最近邻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!