Python 一定距离内的重复去除

Duplicate removal within a certain distance in Python

我有两个 numpy.arrays 点(形状 (m,2) 和 (n,2)),如下所示:

A = numpy.array([[1,2],[3,4]])
B = numpy.array([[5,6],[7,8],[9,2]])

我需要将它们合并到具有下一个条件的数组中:

If there are two points with distance less or equal to epsilon, just leave one

我有这段代码,但它太慢了:

import numpy as np

eps = 0.1
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8],[9,2]])

for point in B:
    if not (np.amin(np.linalg.norm(A-point)) <= eps):
        A = np.append(  A ,  [point], axis=0)

使用 numpy 的最佳方法是什么?

非常感谢!

您可以先计算一个 Delaunay triangulation,从中可以轻松提取相邻点列表:

import numpy as np
from itertools import product
from scipy.spatial import Delaunay

eps = 3.  # choose value, which filters out some points
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8],[9,2]])

# triangulate points:
pts = np.vstack([A, B])
tri = Delaunay(pts)

# extract all edges:
si_idx = [[0, 1], [0, 2], [1, 2]]  # edge indeces in tri.simplices
edges = [si[i] for si, i in product(tri.simplices, si_idx)]
dist_edges = [np.linalg.norm(tri.points[ii[0]] - tri.points[ii[1]])
              for ii in edges]  # calculate distances

# list points which are closer than eps:
for ee, d in zip(edges, dist_edges):
    if d < eps:
        print("|p[{}] - p[{}]| = {}".format(ee[0], ee[1], d))

正如@David Wolever 已经指出的那样,您的问题并不清楚如何从合并列表中准确删除点。