【发布时间】:2023-03-22 09:50:01
【问题描述】:
如何在 Python 中将稀疏的 NDArray 存储在磁盘上?
我正在回答我自己的问题,因为我浪费了将近一周的时间试图从核心矩阵中获取稀疏。也许这对某些人来说是显而易见的,但对我和另一个可怜的人来说却不是!
【问题讨论】:
标签: python multidimensional-array sparse-matrix
如何在 Python 中将稀疏的 NDArray 存储在磁盘上?
我正在回答我自己的问题,因为我浪费了将近一周的时间试图从核心矩阵中获取稀疏。也许这对某些人来说是显而易见的,但对我和另一个可怜的人来说却不是!
【问题讨论】:
标签: python multidimensional-array sparse-matrix
由接受的答案 here 提示,然后使用 h5py 制作的数据集进行测试,以下时间序列测试有效。
>>> f = h5py.File('./test.h5')
>>> d = f.create_dataset('test', (10000, 10000), chunks=(100, 100))
>>> f.flush()
>>> d[1,1] = 1.0
>>> f.flush()
>>> d[2,1] = 1.0
>>> f.flush()
>>> d[2,100] = 1.0
>>> f.flush()
>>> d[2000,100] = 1.0
>>> f.flush()
>>> d[2000,1000] = 1.0
>>> f.flush()
>>>
以下是每次刷新后 bash 报告的文件大小
$ ls -lth test.h5
-rw-rw-r-- 1 aidan aidan 1.4K Jul 28 18:51 test.h5
$ ls -lth test.h5
-rw-rw-r-- 1 aidan aidan 43K Jul 28 18:51 test.h5
$ ls -lth test.h5
-rw-rw-r-- 1 aidan aidan 43K Jul 28 18:52 test.h5
$ ls -lth test.h5
-rw-rw-r-- 1 aidan aidan 83K Jul 28 18:52 test.h5
$ ls -lth test.h5
-rw-rw-r-- 1 aidan aidan 122K Jul 28 18:52 test.h5
$ ls -lth test.h5
-rw-rw-r-- 1 aidan aidan 161K Jul 28 18:53 test.h5
$
可以看出,文件的大小仅以 40Kb(100x100 浮点数)为增量增加,并且仅在生成超出现有块大小的元素时。我们也可以跳过,只制作需要的块(即不是中间块)!
魔法!
【讨论】: