坚持大scipy.sparse.csr_matrix

时间:2018-05-24 20:36:35

标签: python numpy scipy persistence sparse-matrix

我有一个非常大的稀疏scipy矩阵。尝试使用save_npz会导致以下错误:

>>> sp.save_npz('/projects/BIGmatrix.npz',W)
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py", line 716, in _savez
    pickle_kwargs=pickle_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/format.py", line 597, in write_array
    array.tofile(fp)
OSError: 6257005295 requested and 3283815408 written

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/scipy/sparse/_matrix_io.py", line 78, in save_npz
    np.savez_compressed(file, **arrays_dict)
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py", line 659, in savez_compressed
    _savez(file, args, kwds, True)
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py", line 721, in _savez
    raise IOError("Failed to write to %s: %s" % (tmpfile, exc))
OSError: Failed to write to /projects/BIGmatrix.npzg6ub_z3y-numpy.npy: 6257005295 requested and 3283815408 written

因此我想通过psycopg2尝试将其持久化到postgres但我还没有找到一种迭代所有非零的方法,所以我可以将它们作为行保存在表格中。

处理此任务的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

似乎事情的发展方向是:

当您调用scipy.sparse.save_npz()时,默认情况下会将其另存为压缩文件;但是,为了做到这一点,它首先创建一个目标文件的临时未压缩版本,然后将其压缩到最终结果。这意味着你保存的任何驱动器都需要足够大以容纳未压缩的临时文件,在我的情况下是47G。

我在一个更大的驱动器中重新尝试了保存,并且该过程没有发生任何事故。

注意:压缩可能需要很长时间。

相关问题