在阈值以下,是否有更好,更快的方法从Scipy压缩距离矩阵转换为Scipy稀疏距离矩阵

时间:2019-07-04 14:02:30

标签: python optimization scipy euclidean-distance

我正在尝试计算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()

然后将输出输入到库中,当输入为稀疏稀疏距离矩阵形式时,该库会更快。

0 个答案:

没有答案