我正在寻找半径在1公里以内的所有最近的邻居。这是我的脚本,用来构造树和搜索最近的点,
from pysal.cg.kdtree import KDTree
def construct_tree(s):
data_geopoints = [tuple(x) for x in s[['longitude','latitude']].to_records(index=False)]
tree = KDTree(data_geopoints, distance_metric='Arc', radius=pysal.cg.RADIUS_EARTH_KM)
return tree
def get_neighbors(s,tree):
indices = tree.query_ball_point(s, 1)
return indices
#Constructing the tree for search
tree = construct_tree(data)
#Finding the nearest neighbours within 1KM
data['neighborhood'] = data['lat_long'].apply(lambda row: get_neighbors(row,tree))从我在书页上看到的,上面写着-
kd-树建立在kd-树的功能之上。如果使用scipy.spatial.cKDTree 0.12或更高版本,则使用scipy.spatial.KDTree。
在我的例子中,它应该使用cKDTree。对于示例数据集来说,这很好,但是由于tree.query_ball_point返回索引列表。每个列表都有100个元素。对于我的数据点(200万条记录),这是越来越大,并停止,因为内存问题后,一定的点。知道怎么解决这个问题吗?
发布于 2017-08-13 22:39:07
如果有人想找答案的话,我已经解决了这个问题,方法是为一个组找到最近的邻居(tree.query_ball_point可以处理批处理),然后写入数据库,然后处理下一个组,而不是将它们保存在内存中。谢谢。
https://stackoverflow.com/questions/45407356
复制相似问题