我正在尝试查找 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))
从我在 pysal 页面中读到的内容,它说 -
kd-tree 建立在 scipy 中的 kd-tree 功能之上。如果使用 scipy 0.12 或更高版本,则使用 scipy.spatial.cKDTree,否则使用 scipy.spatial.KDTree。
就我而言,它应该使用 cKDTree。这对于示例数据集来说工作得很好,但是由于tree.query_ball_point
返回索引列表作为结果。每个列表将包含 100 个元素。对于我的数据点(200 万条记录),这会变得越来越大,并且在某个点之后由于内存问题而停止。关于如何解决这个问题的任何想法?