Scipy稀疏矩阵行广播

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

我最近一直试图做以下(有效)

  1. 读取稀疏(csr)矩阵
  2. 选择行的子集
  3. 构造另一个矩阵(全为零)
  4. 用2中获得的子集填充3.

我几乎可以达到以下目的:

input_matrix = scipy.io.loadmat(some_matrix)

random_indices = np.random.choice(input_matrix.shape[1], num_samples, replace=False)

second_matrix = sp.dok_matrix(input_matrix.shape)

## this takes up too much memory!
second_matrix[random_indices] = input_matrix[random_indices]

如何更有效地做到这一点?我不想在任何时候调用.todense(),因为这也会在内存中爆炸。直觉上,人们应该能够掩盖矩阵的一部分吗?在numpy(密集)中,我只是用零填充余数,但对于csr矩阵,我不确定这是不是这样。

谢谢!

python scipy
1个回答
0
投票

我测试了.dok和.csr格式,但唯一不会导致空间爆炸的格式是:

second_matrix[random_indices] = input_matrix.tolil()[random_indices]

因此,.lil矩阵。

hpaulj的建议也很有意义,但我还没有测试它(我的任务可以正常工作)。

© www.soinside.com 2019 - 2024. All rights reserved.