我最近一直试图做以下(有效)
我几乎可以达到以下目的:
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矩阵,我不确定这是不是这样。
谢谢!
我测试了.dok和.csr格式,但唯一不会导致空间爆炸的格式是:
second_matrix[random_indices] = input_matrix.tolil()[random_indices]
因此,.lil矩阵。
hpaulj的建议也很有意义,但我还没有测试它(我的任务可以正常工作)。