python数据统计 矢量图_在Python中矢量化Haversine距离计算

我正在尝试计算由纬度和经度识别的一长串位置列表的距离矩阵.使用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),但无法弄清楚如何在这种情况下应用它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值