我有一个N点(N * 3)的3D坐标的数组X,并且想要计算每对点之间的eukledian距离。如何在numPy中执行此操作?
我可以遍历X,并将其与阈值进行比较做到这一点。
coords = array([v.xyz for v in vertices])
for vertice in vertices:
tests = np.sum(array(coords - vertice.xyz) ** 2, 1) < threshold
closest = [v for v, t in zip(vertices, tests) if t]
这是可能在一个操作中做?我记得10年前的线性代数,找不到一种方法来做到这一点。
也许这应该是一个3D阵列(点A,点B,轴),然后通过axis
尺寸相加。
编辑:我自己找到了解决方案,但它不适用于大数据集。
coords = array([v.xyz for v in vertices])
big = np.repeat(array([coords]), len(coords), 0)
big_same = np.swapaxes(big, 0, 1)
tests = np.sum((big - big_same) ** 2, 0) < thr_square
for v, test_vector in zip(vertices, tests):
v.closest = self.filter(vertices, test_vector)
它能保持70k点吗? – 2012-03-14 19:49:50
@culebrón:不要介意我以前的评论(现在删除)。这个规模的问题是记忆;距离矩阵需要GB。但是,如果您不使用'scipy.spatial.distance',问题将是CPU时间。你需要做什么距离? – 2012-03-14 19:58:09
我需要从每个阈值中找出点,以将它们聚类。 – 2012-03-14 20:02:28