【发布时间】:2021-05-19 20:41:53
【问题描述】:
我有一个地点列表,我需要找出每个地点之间的距离。谁能建议一种更快的方法?大约有 10k 个独特的位置,我使用的方法创建了一个 10k X 10k 矩阵,但内存不足。我使用的是 15GB 内存。
test_df
Latitude Longitude site
0 32.3 -94.1 1
1 35.2 -93.1 2
2 33.1 -83.4 3
3 33.2 -94.5 4
test_df = test_df[['site', 'Longitude', 'Latitude']]
test_df['coord'] = list(zip(test_df['Longitude'], test_df['Latitude']))
from haversine import haversine
for _,row in test_df.iterrows():
test_df[row.coord]=round(test_df['coord'].apply(lambda x:haversine(row.coord,x, unit='mi')),2)
df = test_df.rename(columns=dict(zip(test_df['coord'], test_df['Facility'])))
df.drop('coord', axis=1, inplace=True)
new_df = pd.melt(df, id_vars='Facility', value_vars=df.columns[1:])
new_df.rename(columns={'variable':'Place', 'value':'dist_in_mi'}, inplace=True)
new_df
site Place dist_in_mi
0 1 1 0.00
1 2 1 70.21
2 3 1 739.28
3 4 1 28.03
4 1 2 70.21
5 2 2 0.00
6 3 2 670.11
7 4 2 97.15
8 1 3 739.28
9 2 3 670.11
10 3 3 0.00
11 4 3 766.94
12 1 4 28.03
13 2 4 97.15
14 3 4 766.94
15 4 4 0.00
【问题讨论】:
-
我们能否将数据框设为可复制粘贴的形式(例如托管在 github 上)?
-
您要求更快的方法,但您说问题是内存不足
-
你说得对,我需要一个更快的方法,因为我使用它的方式没有经过优化 - 我尝试使用我在这里共享的虚拟集,它有效
-
为了更快的方法,我建议使用多处理并且不要计算与同一地点的距离 2 次span>