问题描述
我有一个地点列表,我需要找出每个地点之间的距离。谁能建议一个更快的方法?大约有 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
解决方法
如果您想解决内存问题,您需要使用使用较少内存的数据类型。
在这种情况下,由于地球上两点之间的最大距离小于 20005Km
,您可以使用 uint16
来存储该值(如果 1Km 的分辨率对您来说足够了)
由于我没有任何数据可以使用,所以我使用以下代码生成了一些数据:
import random
import numpy as np
from haversine import haversine
def getNFacilities(n):
""" returns n random pairs of coordinates in the range [-90,+90]"""
for i in range(n):
yield random.random()*180 - 90,random.random()*180 - 90
facilities = list(getNFacilities(10000))
然后我用两种不同的方式解决了内存问题:
1- 通过将距离数据存储在 uint16
个数字中
def calculateDistance(start,end):
mirror = start is end # if the matrix is mirrored the values are calculated just one time instead of two
out = np.zeros((len(start),len(end)),dtype = np.uint16) # might be better to use empty?
for i,coords1 in enumerate(start[mirror:],mirror):
for j,coords2 in enumerate(end[:mirror and i or None]):
out[i,j] = int(haversine(coords1,coords2))
return out
计算距离后,数组使用的内存约为200MB:
In [133]: l = calculateDistance(facilities,facilities)
In [134]: sys.getsizeof(l)
Out[134]: 200000112
2- 或者,您可以只使用生成器:
def calculateDistance(start,end):
mirror = start is end # if the matrix is mirrored the values are calculated just one time
for i,coords2 in enumerate(end[:mirror and i or None]):
yield [i,j,haversine(coords1,coords2)]