检查点是否在ci范围内

2024-06-28 23:57:25 发布

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

我有一长串已知坐标的H-points。我还有一个TP-points的列表。我想知道H-points是否属于任何(!)TP-point具有一定半径(例如r=5

dfPoints = pd.DataFrame({'H-points' : ['a','b','c','d','e'],
               'Xh' :[10, 35, 52, 78, 9],
               'Yh' : [15,5,11,20,10]})

dfTrafaPostaje = pd.DataFrame({'TP-points' : ['a','b','c','d','e'],
               'Xt' :[15,25,35],
               'Yt' : [15,25,35],
               'M' : [5,2,3]})

def inside_circle(x, y, a, b, r):
    return (x - a)*(x - a) + (y - b)*(y - b) < r*r

我已经开始了,但是。。如果只检查一个TP点,就容易多了。但如果我有1500个和30000个H点,那么我需要更一般的解决方案。 有人能帮忙吗


Tags: dataframe列表def半径pointspointpdxt
2条回答

另一种选择是使用distance_matrixfrom scipy.spatial

dist_mat = distance_matrix(dfPoints [['Xh','Yh']], dfTrafaPostaje [['Xt','Yt']])
dfPoints [np.min(dist_mat,axis=1)<5]

1500 dfPoints30000 dfTrafaPostje需要2秒


更新:获取得分最高的参考点的索引:

dist_mat = distance_matrix(dfPoints [['Xh','Yh']], dfTrafaPostaje [['Xt','Yt']])

# get the M scores of those within range
M_mat = pd.DataFrame(np.where(dist_mat <= 5, dfTrafaPosaje['M'].values[None, :], np.nan),
                     index=dfPoints['H-points'] ,
                     columns=dfTrafaPostaje['TP-points'])

# get the points with largest M values
# mask with np.nan for those outside range    
dfPoints['M'] = np.where(M_mat.notnull().any(1), M_mat.idxmax(1), np.nan)

对于包含的样本数据:

  H-points  Xh  Yh   TP
0        a  10  15    a
1        b  35   5  NaN
2        c  52  11  NaN
3        d  78  20  NaN
4        e   9  10  NaN

您可以使用scipy中的cdist来计算成对距离,然后在距离小于半径的地方创建一个带True的掩码,最后过滤:

import pandas as pd
from scipy.spatial.distance import cdist

dfPoints = pd.DataFrame({'H-points': ['a', 'b', 'c', 'd', 'e'],
                         'Xh': [10, 35, 52, 78, 9],
                         'Yh': [15, 5, 11, 20, 10]})

dfTrafaPostaje = pd.DataFrame({'TP-points': ['a', 'b', 'c'],
                               'Xt': [15, 25, 35],
                               'Yt': [15, 25, 35]})

radius = 5
distances = cdist(dfPoints[['Xh', 'Yh']].values, dfTrafaPostaje[['Xt', 'Yt']].values, 'sqeuclidean')
mask = (distances <= radius*radius).sum(axis=1) > 0 # create mask

print(dfPoints[mask])

输出

  H-points  Xh  Yh
0        a  10  15

相关问题 更多 >