如何在较大空间内将点分组

时间:2019-02-07 08:25:58

标签: python numpy

我在一个2200x1700的空间中有(x,y)坐标的73,000个点,我想将所有(x,y)点归为一组,这些点落入半径为35个单位的圆中。 为此,我必须为时间复杂度为O(73000x73000)的循环运行嵌套,因为我正在计算每个点之间的距离,并对计算出的距离小于35的点进行分组。

我需要为此优化的解决方案!

temp_cluster=cluster
for key,value in cluster.items():
    pointa=np.array((key[0],key[1]))
    for key2,value2 in temp_cluster.items():
        pointb=np.array((key2[0],key2[1]))
        distance=np.linalg.norm(pointa-pointb)
        if(distance<=35):
            cluster[(key[0],key[1])]=cluster[(key[0],key[1])]+1## here we are counting how many points lies within a radius of 35 for that particular point

1 个答案:

答案 0 :(得分:2)

您可以做的一种优化是根据点的x坐标对点进行排序。然后,对于每个点,您只需要考虑在该点的x差之内具有35坐标的点。通过二进制搜索,您可以找到该条带内的点(即,找到点(x1, y1)这样的点abs(x-x1) <= 35)。大致步骤是

sort(points) according to their x coordinates
for each point (x, y):
    count = 0
    start_index = starting index of points with x-x1 >= 35 (find using binary search)
    end_index = end index of point with x1-x<=35 (find using binary search)
    for each point(x1, y1) in (start_index, end_index):
        distance = calculate_distance(x, y, x1, y1)
        if distance <= 35:
            count = count + 1
    cluster[(x,y)] = count