python - h5py:切片数组数据集的正确方法

标签 python numpy h5py

我在这里有点困惑:

据我所知,h5py 的 .value 方法读取整个数据集并将其转储到一个数组中,这是缓慢且不鼓励的(通常应替换为 [() ]。正确的方法是使用 numpy 式切片。

但是,我得到了令人恼火的结果(使用 h5py 2.2.1):

import h5py
import numpy as np
>>> file = h5py.File("test.hdf5",'w')
# Just fill a test file with a numpy array test dataset
>>> file["test"] = np.arange(0,300000)

# This is TERRIBLY slow?!
>>> file["test"][range(0,300000)]
array([     0,      1,      2, ..., 299997, 299998, 299999])
# This is fast
>>> file["test"].value[range(0,300000)]
array([     0,      1,      2, ..., 299997, 299998, 299999])
# This is also fast
>>> file["test"].value[np.arange(0,300000)]
array([     0,      1,      2, ..., 299997, 299998, 299999])
# This crashes
>>> file["test"][np.arange(0,300000)]

我想我的数据集太小了,.value 不会显着影响性能,但第一个选项怎么会那么慢呢? 这里的首选版本是什么?

谢谢!

更新 看来我不够清楚,抱歉。我知道 .value 将整个数据集复制到内存中,而切片仅检索适当的子部分。我想知道的是为什么在文件中切片比复制整个数组然后在内存中切片。 我一直认为 hdf5/h5py 是专门实现的,因此切片子部分总是最快的。

最佳答案

要使用 h5py 进行快速切片,请坚持使用“普通”切片符号:

file['test'][0:300000]

或者,例如,读取所有其他元素:

file['test'][0:300000:2]

简单切片(切片对象和单个整数索引)应该非常快,因为它直接转换为 HDF5 hyperslab 选择。

表达式 file['test'][range(300000)] 调用 h5py 版本的“花式索引”,即通过显式索引列表进行索引。在 HDF5 中没有执行此操作的本地方法,因此 h5py 在 Python 中实现了一个(较慢的)方法,不幸的是,当列表超过 1000 个元素时,它的性能很差。 file['test'][np.arange(300000)] 也是如此,其解释方式相同。

另见:

[1] http://docs.h5py.org/en/latest/high/dataset.html#fancy-indexing

[2] https://github.com/h5py/h5py/issues/293

关于python - h5py:切片数组数据集的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766145/

相关文章:

python - 使用 H5Py 在 HDF5 中存储日期时间

python - 远程构建特定的 Jenkins 分支

python - 如何避免 numpy.exp() 中的溢出

python - 多线程prange循环抛出 "double free or corruption (fasttop)"错误

python - 在 h5py 中压缩文件更大

python - TensorFlow 2.x : Cannot load trained model in h5 format when using embedding columns (ValueError: Shapes (101, 15) 和 (57218, 15) 不兼容)

javascript - 在 PyCrypto AES MODE_CTR 中包含 nonce 和 block 计数

python - 从字符串文字设置变量名时如何编写 DRY 代码?

python - 为什么python xmlrpc调用在通过apache运行时会得到PermissionError?

python - 找到二维直方图的峰值