亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查點是否在 circleS 內

檢查點是否在 circleS 內

慕雪6442864 2022-06-14 10:53:23
我有一長串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 個和 30.000 個 H 點,那么我需要更通用的解決方案。任何人都可以幫忙嗎?
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

另一種選擇是使用distance_matrixfrom scipy.spatial:


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

dfPoints [np.min(dist_mat,axis=1)<5]

1500 dfPoints和花了大約 2 秒30000 dfTrafaPostje。


更新:獲取得分最高的參考點的索引:


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


查看完整回答
反對 回復 2022-06-14
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

您可以使用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


查看完整回答
反對 回復 2022-06-14
  • 2 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號