CSR矩阵中行和列的重新排序

问题描述 投票:1回答:1

例如,我有一个稀疏csr格式的矩阵:

from scipy.sparse import csr_matrix
import numpy as np
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
M  = csr_matrix((data, (row, col)), shape=(3, 3)) 
M.A
    array([[6, 4, 5],
           [2, 1, 0],
           [3, 0, 0]])

我正在使用以下方法对索引为[2,0,1]的矩阵进行重新排序:

order = np.array([2,0,1])
M = M[order,:]
M = M[:,order]
M.A
array([[6, 4, 5],
       [2, 1, 0],
       [3, 0, 0]])

此方法有效,但对于我的实际csr_matrix来说不可行,它的大小为16580746 X 1672751804,并导致内存错误。有什么建议可以有效地做到这一点?

python out-of-memory sparse-matrix large-data adjacency-matrix
1个回答
0
投票

让我们觉得聪明。

而不是重新排序矩阵,为什么不直接处理开始时提供的行和列索引?

例如,您可以通过以下方式替换行索引:

[0, 0, 1, 2, 2, 2]

至:

[1, 1, 2, 0, 0, 0]

以及您的列索引,来自:

[0, 2, 2, 1, 1, 1]

至:

[1, 0, 0, 2, 2, 2]
© www.soinside.com 2019 - 2024. All rights reserved.