快速半正弦近似(Python/Pandas)

新手上路,请多包涵

Pandas 数据框中的每一行都包含 2 个点的纬度/经度坐标。使用下面的 Python 代码,计算许多(数百万)行的这两个点之间的距离需要很长时间!

考虑到 2 个点相距不到 50 英里,精度不是很重要,是否可以加快计算速度?

 from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))
    km = 6367 * c
    return km

for index, row in df.iterrows():
    df.loc[index, 'distance'] = haversine(row['a_longitude'], row['a_latitude'], row['b_longitude'], row['b_latitude'])

原文由 Nyxynyx 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 512
1 个回答

这是同一函数的矢量化 numpy 版本:

 import numpy as np

def haversine_np(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)

    All args must be of equal length.

    """
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2

    c = 2 * np.arcsin(np.sqrt(a))
    km = 6367 * c
    return km

输入都是值的数组,它应该能够立即完成数百万个点。要求是输入是 ndarrays 但 pandas 表的列将起作用。

例如,使用随机生成的值:

 >>> import numpy as np
>>> import pandas
>>> lon1, lon2, lat1, lat2 = np.random.randn(4, 1000000)
>>> df = pandas.DataFrame(data={'lon1':lon1,'lon2':lon2,'lat1':lat1,'lat2':lat2})
>>> km = haversine_np(df['lon1'],df['lat1'],df['lon2'],df['lat2'])

或者,如果您想创建另一列:

 >>> df['distance'] = haversine_np(df['lon1'],df['lat1'],df['lon2'],df['lat2'])

在 python 中循环遍历数据数组非常慢。 Numpy 提供了对整个数据数组进行操作的函数,这使您可以避免循环并显着提高性能。

这是 矢量化 的一个例子。

原文由 derricw 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题