我试图在最大距离内找到(x,y)点对。我认为最简单的方法就是生成一个数据帧,逐个遍历每个点,计算在给定点(x_0,y_0)的距离r内是否有坐标(x,y)的点。然后,将发现的对的总数除以2。在
%pylab inline
import pandas as pd
def find_nbrs(low, high, num, max_d):
x = random.uniform(low, high, num)
y = random.uniform(low, high, num)
points = pd.DataFrame({'x':x, 'y':y})
tot_nbrs = 0
for i in arange(len(points)):
x_0 = points.x[i]
y_0 = points.y[i]
pt_nbrz = points[((x_0 - points.x)**2 + (y_0 - points.y)**2) < max_d**2]
tot_nbrs += len(pt_nbrz)
plot (pt_nbrz.x, pt_nbrz.y, 'r-')
plot (points.x, points.y, 'b.')
return tot_nbrs
print find_nbrs(0, 1, 50, 0.1)
首先,它并不总是能找到正确的对(我看到在指定距离内的点没有标记)。
如果我写plot(..., 'or')
,它会突出显示所有的点。这意味着pt_nbrz = points[((x_0 - points.x)**2 + (y_0 - points.y)**2) < max_d**2]
至少返回一个(x,y)。为什么?如果比较结果为False,它不应该返回一个空数组吗?
我如何在熊猫身上更优雅地完成以上所有的事情?例如,不必遍历每个元素。
您要查找的功能包含在scipy's spatial distance module中。在
下面是一个如何使用它的示例。真正的魔力在于
squareform(pdist(points))
。在There are 27 other points within a distance of 0.1 from the point (0.194, 0.160)
出于可视化目的:
^{pr2}$相关问题 更多 >
编程相关推荐