Python 如何在一个较大的空间范围内将点编组在一起
我有73000个点,在2200x1700的空间中有(x,y)坐标,我想把所有(x,y)点组合在一起,它们落在半径为35个单位的圆中。 为此,我必须运行时间复杂度为O(73000x73000)的嵌套for循环,因为我正在计算每个点之间的距离,并对计算距离小于35的点进行分组 我需要一个优化的解决方案Python 如何在一个较大的空间范围内将点编组在一起,python,numpy,Python,Numpy,我有73000个点,在2200x1700的空间中有(x,y)坐标,我想把所有(x,y)点组合在一起,它们落在半径为35个单位的圆中。 为此,我必须运行时间复杂度为O(73000x73000)的嵌套for循环,因为我正在计算每个点之间的距离,并对计算距离小于35的点进行分组 我需要一个优化的解决方案 temp_cluster=cluster for key,value in cluster.items(): pointa=np.array((key[0],key[1])) for
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
temp_cluster=cluster
对于键,值位于cluster.items()中:
pointa=np.array((键[0],键[1]))
对于键2,temp_cluster.items()中的值2:
pointb=np.array((键2[0],键2[1]))
距离=np.linalg.norm(点A点B)
如果(距离你可以做的一个优化是根据它们的代码< x>代码>坐标来排序这些点,那么对于每个点,你只需要考虑在代码< <代码> 35 中的点,在这一点上的差异。用二进制搜索,你可以找到这个条带内的点(即,找到点<代码>)。(x1,y1)
使得abs(x-x1)=35(使用二进制搜索查找)
Endoindex = X1-X点的结束索引。你可以做的一个优化是根据它们的代码< x>代码>坐标来排序这些点。然后对于每一点,你需要考虑的只是点有<代码> x<代码>坐标在<代码> 35 这一点的差异。用二进制搜索,你可以找到在这个条带内的点。(即,查找点
(x1,y1)
,以便abs(x-x1)=35(使用二进制搜索查找)
end_index=点的end index与x1 x在计算复杂度时,可以说“it in O(n*n)”而不是用73000指定n在计算复杂度时,可以说“it in O(n*n)”而不是用73000指定n你考虑过排序步骤的时间复杂度吗?通常是O(n**2)。我认为可以使用Python在O(n log n)
中进行排序。您考虑过排序步骤的时间复杂性吗?通常是O(n**2)。我认为可以使用Python在O(n log n)
中进行排序
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