小编ram*_*mos的帖子

如何使用置换数组有效地置换稀疏(Numpy)矩阵中的行?

我使用 Scipy Reverse Cuthill-McKee 实现(scipy.sparse.csgraph.reverse_cuthill_mckee)使用(高维)稀疏 csr_matrix 创建带矩阵。这种方法的结果是一个排列数组,它为我提供了如何排列我所理解的矩阵行的索引。

现在是否有任何有效的解决方案可以在任何其他稀疏矩阵(csr、lil_matrix 等)中对我的稀疏 csr_matrix 进行这种排列?我尝试了一个 for 循环,但我的矩阵的尺寸为 200,000 x 150,000,并且需要太多时间。

A = csr_matrix((data,(rowind,columnind)), shape=(200000, 150000), dtype=np.uint8)

permutation_array = csgraph.reverse_cuthill_mckee(A, false)

result_matrix = lil_matrix((200000, 150000), dtype=np.uint8)

i=0
for x in np.nditer(permutation_array):
    result_matrix[x, :]=A[i, :]
    i+=1
Run Code Online (Sandbox Code Playgroud)

reverse_cuthill_mckee 调用的结果是一个数组,它就像一个包含我的排列索引的 tupel。所以这个数组类似于:[199999 54877 54873 ..., 12045 9191 0] (size = 200,000)

这意味着:索引为 0 的行现在索引为 199999,索引为 1 的行现在索引为 54877,索引为 2 的行现在索引为 54873,等等。请参见:https ://en.wikipedia.org/wiki/Permutation#Definition_and_notations (正如我所理解的回报)

谢谢

python numpy permutation scipy sparse-matrix

3
推荐指数
1
解决办法
2154
查看次数

标签 统计

numpy ×1

permutation ×1

python ×1

scipy ×1

sparse-matrix ×1