gpt4 book ai didi

Python 任意列的矩阵和

转载 作者:行者123 更新时间:2023-11-30 22:24:45 25 4
gpt4 key购买 nike

我正在编写一个算法,我需要根据不同节点的集群分配来“折叠”或“减少”矩阵。然而,目前的实现是我的完整算法的瓶颈(在 Visual Studio Python 分析器中测试)。

def reduce_matrix(mat: np.matrix, cluster_ids: np.array) -> np.matrix:
"""Reduce node adjacency matrix.

Arguments:
mat: Adjacency matrix
cluster_ids: Cluster membership assignment per current node (integers)

Returns:
Reduced adjacency matrix
"""

ordered_nodes = np.argsort(cluster_ids)
counts = np.unique(cluster_ids, return_counts=True)[1]

ends = np.cumsum(counts)
starts = np.concatenate([[0], ends[:-1]])

clusters = [ordered_nodes[start:end] for start, end in zip(starts, ends)]

n_c = len(counts)

reduced = np.mat(np.zeros((n_c, n_c), dtype=int))
for a in range(n_c):
a_nodes = clusters[a]
for b in range(a + 1, n_c):
b_nodes = clusters[b]
reduced[a, b] = np.sum(mat[a_nodes, :][:, b_nodes])
reduced[b, a] = np.sum(mat[b_nodes, :][:, a_nodes])

return reduced

对矩阵中任意行和列求和的最快方法是什么?

我相信双索引[a_nodes, :][:, b_nodes]会创建矩阵的副本而不是 View ,但我不确定是否有更快的解决方法。 ..

最佳答案

Numba 可以以非常自然的方式加速此类任务,并且没有排序问题。这里,必须管理大量不规则 block ,因此 Numpy 效率不是很高:

@numba.jit  
def reduce_matrix2(mat, cluster_ids):
n_c=len(set(cluster_ids))
out = np.zeros((n_c, n_c), dtype=int)
for i,i_c in enumerate(cluster_ids):
for j,j_c in enumerate(cluster_ids):
out[i_c,j_c] += mat[i,j]
np.fill_diagonal(out,0)
return out

5000x5000 垫子上:

In [40]: %timeit r=reduce_matrix2(mat,cluster_ids)
30.3 ms ± 5.34 ms per loop (mean ± std. dev. of 7 runs, 10 loop each)

关于Python 任意列的矩阵和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com