到同一 DF 中最近点的距离
Distance to Closest Point in Same DF
我有一个带有对象 ID、纬度和经度的 df。我想创建两个新列:到最近点的距离和最近点的对象 ID。
df[['OBJECT_ID','Lat','Long']].head()
OBJECT_ID Lat Long
0 33007002190000.0 47.326963 -103.079835
1 33007007900000.0 47.259770 -103.040797
2 33007008830000.0 47.296953 -103.099424
3 33007012130000.0 47.256700 -103.597082
4 33007013320000.0 46.996013 -103.452384
如何在 Python 中使用任何库完成此操作?此外,如果有帮助,我的 DF 包含几千行。
你可以用scipy's KDTree。
非常适合空间距离查询。
使用您的示例数据,您可以执行类似
的操作
import scipy
coordinates = df[["Lat", "Long"]]
# build kdtree
kdtree = scipy.spatial.cKDTree(coordinates)
# query the same tree with the same coordinates. NOTICE the k=2
distances, indexes = kdtree.query(coordinates, k=2)
# assign it to a new dataframe (NOTICE the index of 1)
new_df = df.assign(ClosestID=df["OBJECT_ID"][indexes[:,1]].array)
new_df = new_df.assign(ClosestDist=distances[:,1])
结果为
>> new_df
OBJECT_ID Lat Long ClosestID ClosestDist
0 33007002190000.0 47.326963 -103.079835 33007008830000.0 0.035838
1 33007007900000.0 47.259770 -103.040797 33007008830000.0 0.069424
2 33007008830000.0 47.296953 -103.099424 33007002190000.0 0.035838
3 33007012130000.0 47.256700 -103.597082 33007013320000.0 0.298153
4 33007013320000.0 46.996013 -103.452384 33007012130000.0 0.298153
之所以使用k=2
是因为最近的距离(当用相同的坐标查询时)总是同一个点。即:
>> kdtree.query(coordinates, k=2)
# this is distance
(array([[0. , 0.03583754],
[0. , 0.06942406],
[0. , 0.03583754],
[0. , 0.29815302],
[0. , 0.29815302]]),
# ^ ^
# | |
# closest second-closest
# this is indexes
array([[0, 2],
[1, 2],
[2, 0],
[3, 4],
[4, 3]]))
离每个点最近的点就是它自己。因此,我们忽略第一个元素并使用 index=1 来检索第二个最近点(即除自身以外的最近点)。
我有一个带有对象 ID、纬度和经度的 df。我想创建两个新列:到最近点的距离和最近点的对象 ID。
df[['OBJECT_ID','Lat','Long']].head()
OBJECT_ID Lat Long
0 33007002190000.0 47.326963 -103.079835
1 33007007900000.0 47.259770 -103.040797
2 33007008830000.0 47.296953 -103.099424
3 33007012130000.0 47.256700 -103.597082
4 33007013320000.0 46.996013 -103.452384
如何在 Python 中使用任何库完成此操作?此外,如果有帮助,我的 DF 包含几千行。
你可以用scipy's KDTree。 非常适合空间距离查询。
使用您的示例数据,您可以执行类似
的操作import scipy
coordinates = df[["Lat", "Long"]]
# build kdtree
kdtree = scipy.spatial.cKDTree(coordinates)
# query the same tree with the same coordinates. NOTICE the k=2
distances, indexes = kdtree.query(coordinates, k=2)
# assign it to a new dataframe (NOTICE the index of 1)
new_df = df.assign(ClosestID=df["OBJECT_ID"][indexes[:,1]].array)
new_df = new_df.assign(ClosestDist=distances[:,1])
结果为
>> new_df
OBJECT_ID Lat Long ClosestID ClosestDist
0 33007002190000.0 47.326963 -103.079835 33007008830000.0 0.035838
1 33007007900000.0 47.259770 -103.040797 33007008830000.0 0.069424
2 33007008830000.0 47.296953 -103.099424 33007002190000.0 0.035838
3 33007012130000.0 47.256700 -103.597082 33007013320000.0 0.298153
4 33007013320000.0 46.996013 -103.452384 33007012130000.0 0.298153
之所以使用k=2
是因为最近的距离(当用相同的坐标查询时)总是同一个点。即:
>> kdtree.query(coordinates, k=2)
# this is distance
(array([[0. , 0.03583754],
[0. , 0.06942406],
[0. , 0.03583754],
[0. , 0.29815302],
[0. , 0.29815302]]),
# ^ ^
# | |
# closest second-closest
# this is indexes
array([[0, 2],
[1, 2],
[2, 0],
[3, 4],
[4, 3]]))
离每个点最近的点就是它自己。因此,我们忽略第一个元素并使用 index=1 来检索第二个最近点(即除自身以外的最近点)。