Python 有没有更好更快的方法在阈值下将Scipy压缩距离矩阵转换为Scipy稀疏距离矩阵

Python 有没有更好更快的方法在阈值下将Scipy压缩距离矩阵转换为Scipy稀疏距离矩阵,python,optimization,scipy,euclidean-distance,Python,Optimization,Scipy,Euclidean Distance,我试图计算n维点之间的欧几里德距离,然后得到距离低于设定阈值的所有点的稀疏距离矩阵 我已经有了一个有效的方法,但它太慢了。对于12000个3D点,大约需要8秒。程序的其余部分在一秒钟内运行,因此这是主要的瓶颈。这将运行数百次,因此改进此步骤将大大提高性能 这是我当前的实现 def make_sparse_dm(points: np.array, thresh): n = points.shape[0] distance_matrix = spatial.dist

我试图计算n维点之间的欧几里德距离,然后得到距离低于设定阈值的所有点的稀疏距离矩阵

我已经有了一个有效的方法,但它太慢了。对于12000个3D点,大约需要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()
def make_sparse_dm(点:np.array,thresh):
n=点。形状[0]
距离矩阵=
空间.distance.squareform(空间.distance.pdist(点))
#成对距离(点)
[i,j]=np.meshgrid(np.arange(n),np.arange(n))

_thresh=distance_矩阵下的points_我想知道
pdist
是大时间消费者,还是创建稀疏矩阵。@hpaulj从我的计时来看,它们大致相同,都需要大约4秒的时间
point_tree=space.cKDTree(points);点树。稀疏距离矩阵(点树,thresh)。tocsr()是写这篇文章的精辟方法,但它似乎比
make\u sparse\u dm
快。