我正在尝试计算n维点之间的欧几里得距离,然后获得距离低于设定阈值的所有点的稀疏距离矩阵。
我已经有一种方法在工作,但是太慢了。对于3D中的12000点,大约需要8秒钟。该程序的其余部分在不到一秒钟的时间内运行,因此这是主要瓶颈。这将运行数百次,因此改进此步骤将大大提高性能。
这是我目前的实现方式。
def make_sparse_dm(points: np.array, thresh):
n = points.shape[0]
distance_matrix =
spatial.distance.squareform(spatial.distance.pdist(points))
# pairwise_distances(points)
[i, j] = np.meshgrid(np.arange(n), np.arange(n))
points_under_thresh = distance_matrix <= thresh
i = i[points_under_thresh]
j = j[points_under_thresh]
v = distance_matrix[points_under_thresh]
return sparse.coo_matrix((v, (i, j)), shape=(n, n)).tocsr()
然后将输出输入到库中,当输入为稀疏稀疏距离矩阵形式时,该库会更快。