计算 pandas 地理密度的有效方法? [英] Efficient way to calculate geographic density in Pandas?

查看:104
本文介绍了计算 pandas 地理密度的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与美国快餐店相对应的经度和纬度数据列表.对于每个快餐店,我想知道5英里范围内还有多少其他快餐店.我可以像这样使用Geopy在Pandas中进行计算(DataFrame中的每一行都是不同的快餐店):

I have a large list of longitude and latidue data corresponding to fast food places in the U.S. For each fast food place, I want to know how many other fast food places are within 5 miles. I could calculate this in Pandas using Geopy like so (each row in the DataFrame is a different fast food place):

import pandas as pd
import geopy.distance

df = pd.DataFrame({'Fast Food Place':[1,2,3], 'Lat':[33,34,35], 'Lon':[42,43,44]})

for index1, row1 in df.iterrows():
    num_fastfood = 0

    for index2, row2 in df.iterrows():
        # calculate distance in miles between longitude and latitude
        dist = geopy.distance.VincentyDistance(row1[['Lat','Lon']],
                                               row2[['Lat','Lon']]).miles

        # if fast food is within 5 miles, increment num_fastfood
        if dist < 5: # if less than five miles
            num_fastfood = num_fastfood + 1

    df.loc[index1, 'num_fastfood_5miles'] = num_fastfood - 1 # (subtract 1 to exclude self)

但是在非常大的数据集(即50,000行)上,这非常慢.我考虑过使用KDTree进行搜索,但是好奇是否其他人可以使用一种更快的方法?

But this is extremely slow on very large data sets (i.e. 50,000 rows). I considered using a KDTree for the search, but curious if other people have a much quicker method?

推荐答案

scipy.spatial.cKDTree的实现:

from scipy.spatial import cKDTree

def find_neighbours_within_radius(xy, radius):
    tree = cKDTree(xy)
    within_radius = tree.query_ball_tree(tree, r=radius)
    return within_radius

def flatten_nested_list(nested_list):
    return [item for sublist in nested_list for item in sublist]

def total_neighbours_within_radius(xy, radius):
    neighbours = find_neighbours_within_radius(xy, radius)
    return len(flatten_nested_list(neighbours))

这篇关于计算 pandas 地理密度的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆