【问题标题】:How can i select equal distances points from a set of points using python如何使用python从一组点中选择等距离点
【发布时间】:2021-03-03 15:54:45
【问题描述】:

假设一个点或节点的列表。他们每个人都有 x y 和 z 坐标。两点 i 和 j 之间的距离等于D(i,j)= sqrt((xi-xj)^2+(yi-yj)^2+(zi-zj)^2)。这里我得到了 400000 个数据点。

现在,我想选择一组它们之间具有相等距离的节点(之前指定的间距 --> 0.05)。因此选择的点是均匀分布的。

如果使用 while 循环运行,完成整个数据集大约需要 3 小时。 寻找最快的方法。

no_rows = len(df)
i = 1
while i < no_rows:
    a1 = df.iloc[i-1, 1]
    a2 = df.iloc[i, 1]
    b1 = df.iloc[i-1, 2]
    b2 = df.iloc[i, 2]
    c1 = df.iloc[i-1, 3]
    c2 = df.iloc[i, 3]
    dist = np.round(((a2-a1)**2+(b2-b1)**2+(c2-c1)**2)**0.5,5)
    df.iloc[i, 6]= dist
    if dist < 0.05000:
                df = df.drop(i)
                df.reset_index(drop = True, inplace = True)
                no_rows = len(df)
                i = i-1
    i+=1

【问题讨论】:

  • 嗨@Arun,看看这个方法,看看它是否适合你:stackoverflow.com/questions/1401712/…。我认为答案可能是将您的数据放入一个数组中,然后使用 numpy 的矢量化操作来加快速度。

标签: python pandas scipy spatial kdtree


【解决方案1】:

编辑

一种选择是直接使用 pandas 并将数据框合并到自身之上。类似的东西:

import pandas as pd
import numpy as np

df = pd.DataFrame([
  [131.404866,16.176877,128.120177 ], 
  [131.355045,16.176441,128.115972 ], 
  [131.305224,16.176005,128.111767 ], 
  [131.255403,16.175569,128.107562 ], 
  [131.205582,16.175133,128.103357 ], 
  [131.158858,16.174724,128.099413 ], 
  [131.15576,16.174702,128.09916 ], 
  [131.105928,16.174342,128.095089 ], 
  [131.05988,16.174009,128.091328 ], 
  [131.056094,16.173988,128.09103 ], 
  [131.006249,16.173712,128.087107 ], 
  [130.956404,16.173436,128.083184], 
  ],
  columns=['x', 'y', 'z']
)
df.reset_index(drop=False, inplace=True)

dist = 0.05
df['CROSS'] = 1
df = df.merge(df, on="CROSS")
df.reset_index(drop=True, inplace=True)

df['distance'] = np.round(
    np.sqrt(
      np.square(df['x_x'] - df['x_y'])
      + np.square(df['y_x']-df['y_y'])
      + np.square(df['z_x']-df['z_y'])
    ),
    5
)

#drop values where distances are = 0 (same points)
ix = df[df.distance==0].index 
df.drop(ix, inplace=True)


print('These are all pair of points which are matching the distance', dist)
ix = df[df.distance.astype(float)==dist].index
df.sort_values('distance', inplace=True)
print(df.loc[ix])
print('-'*50)


points = pd.DataFrame(
        df.loc[ix, ['index_x', 'x_x', 'y_x', 'z_x']].values.tolist() 
        + df.loc[ix, ['index_y', 'x_y', 'y_y', 'z_y']].values.tolist(), 
        columns=['index', 'x', 'y', 'z'])
points.drop_duplicates(keep='first', inplace=True)
print('These are all the points which have another at distance', dist)
print(points)

Numpy 的函数比任何循环都快得多,并且可以让您同时处理整个数据集。

另一个可能是使用 geopandas(它也可以非常快,但我不确定这里是否会出现这种情况:最快的方法涉及 pyproj 的距离计算(用 C 编写),我认为没有3D 中的任何偏角)

【讨论】:

  • 很好的答案 - 只是最后一次调用 np.square 时的一个小错字。另外,我认为 OP 可能有兴趣保留距离 >= dist 的行,因此它只是在分配给 ix 时用 &gt;= 替换 ==
  • @user6386471 关于错字,谢谢!关于距离的运算符:是的,这是代码中的内容,但不是问题中的内容......所以我会让他检查它是什么:-)
  • 好点!让我们看看@Arun 带来了什么:D
  • 以上代码不起作用。尝试了选项距离 == 0.05 和距离 >=0.05。
  • 你必须给我更多的信息来帮助我。有什么错误?
猜你喜欢
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
  • 2020-05-15
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
相关资源
最近更新 更多