在Python中,我有三个包含x和y坐标的列表。每个列表包含128个点。如何以有效的方式找到最接近的三个点?
这是我正在使用的python代码,但效率不高:
def findclosest(c1, c2, c3):
mina = 999999999
for i in c1:
for j in c2:
for k in c3:
# calculate sum of distances between points
d = xy3dist(i,j,k)
if d < mina:
mina = d
def xy3dist(a, b, c):
l1 = math.sqrt((a[0]-b[0]) ** 2 + (a[1]-b[1]) ** 2 )
l2 = math.sqrt((b[0]-c[0]) ** 2 + (b[1]-c[1]) ** 2 )
l3 = math.sqrt((a[0]-c[0]) ** 2 + (a[1]-c[1]) ** 2 )
return l1+l2+l3
知道如何使用numpy完成这项工作吗?
答案 0 :(得分:3)
您可以使用Numpy的广播功能来矢量化两个内部循环:
import numpy as np
def findclosest(c1, c2, c3):
c1 = np.asarray(c1)
c2 = np.asarray(c2)
c3 = np.asarray(c3)
for arr in (c1, c2, c3):
if not (arr.ndim == 2 and arr.shape[1] == 2):
raise ValueError("expected arrays of 2D coordinates")
min_val = np.inf
min_pos = None
for a, i in enumerate(c1):
d = xy3dist(i, c2.T[:,:,np.newaxis], c3.T[:,np.newaxis,:])
k = np.argmin(d)
if d.flat[k] < min_val:
min_val = d.flat[k]
b, c = np.unravel_index(k, d.shape)
min_pos = (a, b, c)
print a, min_val, d.min()
return min_val, min_pos
def xy3dist(a, b, c):
l1 = np.sqrt((a[0]-b[0]) ** 2 + (a[1]-b[1]) ** 2 )
l2 = np.sqrt((b[0]-c[0]) ** 2 + (b[1]-c[1]) ** 2 )
l3 = np.sqrt((a[0]-c[0]) ** 2 + (a[1]-c[1]) ** 2 )
return l1+l2+l3
np.random.seed(1234)
c1 = np.random.rand(5, 2)
c2 = np.random.rand(9, 2)
c3 = np.random.rand(7, 2)
val, pos = findclosest(c1, c2, c3)
a, b, c = pos
print val, xy3dist(c1[a], c2[b], c3[c])
也可以对所有3个循环进行矢量化
def findclosest2(c1, c2, c3):
c1 = np.asarray(c1)
c2 = np.asarray(c2)
c3 = np.asarray(c3)
d = xy3dist(c1.T[:,:,np.newaxis,np.newaxis], c2.T[:,np.newaxis,:,np.newaxis], c3.T[:,np.newaxis,np.newaxis,:])
k = np.argmin(d)
min_val = d.flat[k]
a, b, c = np.unravel_index(k, d.shape)
min_pos = (a, b, c)
return min_val, min_pos
如果您的数组非常大,findclosest
可能比findclosest2
更好,因为它使用更少的内存。 (如果你的数组很大,只能矢量化一个最里面的循环。)
您可以谷歌搜索“numpy broadcast”以了解更多np.newaxis所做的事情
答案 1 :(得分:2)
让我们尝试一些不同的解决方案来看看。
我将使用numpy的随机函数初始化三个数组。如果现有变量是元组列表或列表列表,只需在它们上面调用np.array
。
import numpy as np
c1 = np.random.normal(size=(128, 2))
c2 = np.random.normal(size=(128, 2))
c3 = np.random.normal(size=(128, 2))
首先让我们为您的代码计时,以便我们有一个起点。
def findclosest(c1, c2, c3):
mina = 999999999
for i in c1:
for j in c2:
for k in c3:
# calculate sum of distances between points
d = xy3dist(i,j,k)
if d < mina:
mina = d
return mina
def xy3dist(a, b, c):
l1 = math.sqrt((a[0]-b[0]) ** 2 + (a[1]-b[1]) ** 2 )
l2 = math.sqrt((b[0]-c[0]) ** 2 + (b[1]-c[1]) ** 2 )
l3 = math.sqrt((a[0]-c[0]) ** 2 + (a[1]-c[1]) ** 2 )
return l1+l2+l3
%timeit findclosest(c1, c2, c3)
# 1 loops, best of 3: 23.3 s per loop
可能有用的一个函数是scipy.spatial.distance.cdist
,它计算两个点阵列之间的所有成对距离。因此我们可以使用它来预先计算和存储所有距离,然后简单地获取并添加这些数组的距离。我也将使用itertools.product
来简化循环,但它不会进行任何加速工作。
from scipy.spatial.distance import cdist
from itertools import product
def findclosest_usingcdist(c1, c2, c3):
dists_12 = cdist(c1, c2)
dists_23 = cdist(c2, c3)
dists_13 = cdist(c1, c3)
min_dist = np.inf
ind_gen = product(range(len(c1)), range(len(c2)), range(len(c3)))
for i1, i2, i3 in ind_gen:
dist = dists_12[i1, i2] + dists_23[i2, i3] + dists_13[i1, i3]
if dist < min_dist:
min_dist = dist
min_points = (c1[i1], c2[i2], c3[i3])
return min_dist, min_points
%timeit findclosest_usingcdist(c1, c2, c3)
# 1 loops, best of 3: 2.02 s per loop
因此,使用cdist
可以为我们带来一个数量级的加速。
def findclosest2(c1, c2, c3):
d = xy3dist(c1.T[:,:,np.newaxis,np.newaxis],
c2.T[:,np.newaxis,:,np.newaxis],
c3.T[:,np.newaxis,np.newaxis,:])
k = np.argmin(d)
min_val = d.flat[k]
i1, i2, i3 = np.unravel_index(k, d.shape)
min_points = (c1[i1], c2[i2], c3[i3])
return min_val, min_points
def xy3dist(a, b, c):
l1 = np.sqrt((a[0]-b[0]) ** 2 + (a[1]-b[1]) ** 2 )
l2 = np.sqrt((b[0]-c[0]) ** 2 + (b[1]-c[1]) ** 2 )
l3 = np.sqrt((a[0]-c[0]) ** 2 + (a[1]-c[1]) ** 2 )
return l1+l2+l3
%timeit findclosest_usingbroadcasting(c1, c2, c3)
# 100 loops, best of 3: 19.1 ms per loop
这是一个巨大的加速,绝对是正确的答案。