Python HDF5稀疏核心外数据集

2024-06-18 11:30:52 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中如何在磁盘上存储稀疏的ndarray?你知道吗

我在回答我自己的问题,因为我浪费了将近一个星期的时间试图从核心矩阵中得到稀疏的信息。也许这对某些人来说是显而易见的,但对我和另一个可怜的灵魂来说却不是!你知道吗


Tags: 信息核心时间浪费矩阵磁盘ndarray灵魂
1条回答
网友
1楼 · 发布于 2024-06-18 11:30:52

在公认答案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浮点)的增量增加,并且仅当元素超出现有块的大小时才增加。我们也可以跳过,只做需要的块(即不是中间块)!你知道吗

魔法!你知道吗

相关问题 更多 >