您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中矢量化Haversine距离计算

5b51 2022/1/14 8:20:54 python 字数 3468 阅读 517 来源 www.jb51.cc/python

我正在尝试计算由纬度和经度识别的一长串位置列表的距离矩阵.使用Haversine公式的经度,该公式采用两个坐标对元组来产生距离:def haversine(point1, point2, miles=False): ''' Calculate the great-circle distance bewteen two points on the Ea

概述

我正在尝试计算由纬度和经度识别的一长串位置列表的距离矩阵.使用Haversine公式的经度,该公式采用两个坐标对元组来产生距离:

def haversine(point1,point2,miles=False):
    """ Calculate the great-circle distance bewteen two points on the Earth surface.

    :input: two 2-tuples,containing the latitude and longitude of each point
    in decimal degrees.

    Example: haversine((45.7597,4.8422),(48.8567,2.3508))

    :output: Returns the distance bewteen the two points.
    The default unit is kilometers. Miles can be returned
    if the ``miles`` parameter is set to True.

    """

我可以使用嵌套for循环计算所有点之间的距离,如下所示:

data.head()

   id                      coordinates
0   1   (16.3457688674,6.30354512503)
1   2    (12.494749307,28.6263955635)
2   3    (27.794615136,60.0324947881)
3   4   (44.4269923769,110.114216113)
4   5  (-69.8540884125,87.9468778773)

使用简单的功能

distance = {}
def haver_loop(df):
    for i,point1 in df.iterrows():
        distance[i] = []
        for j,point2 in df.iterrows():
            distance[i].append(haversine(point1.coordinates,point2.coordinates))

    return pd.DataFrame.from_dict(distance,orient='index')

但考虑到时间的复杂性,这需要相当长的一段时间,在20分钟左右运行500分,而且我有更长的清单.这让我看着矢量化,我遇到了numpy.vectorize((docs),但无法弄清楚如何在这种情况下应用它.

haver_vec = np.vectorize(haversine,otypes=[np.int16])
distance = df.groupby('id').apply(lambda x: pd.Series(haver_vec(df.coordinates,x.coordinates)))

例如,样本数据如下:

length = 500
df = pd.DataFrame({'id':np.arange(length),'coordinates':tuple(zip(np.random.uniform(-90,90,length),np.random.uniform(-180,180,length)))})

比较500分:

def haver_vect(data):
    distance = data.groupby('id').apply(lambda x: pd.Series(haver_vec(data.coordinates,x.coordinates)))
    return distance

%timeit haver_loop(df): 1 loops,best of 3: 35.5 s per loop

%timeit haver_vect(df): 1 loops,best of 3: 593 ms per loop

总结

以上是编程之家为你收集整理的在Python中矢量化Haversine距离计算全部内容,希望文章能够帮你解决在Python中矢量化Haversine距离计算所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶