【问题标题】:Read from a large file without loading whole thing into memory using h5py使用 h5py 从大文件中读取而不将整个内容加载到内存中
【发布时间】:2017-06-16 09:19:01
【问题描述】:

是否从数据集立即将整个事物加载读取到内存中[整个事物将不适合内存]并获取数据集的大小没有在 python 中使用 h5py 加载数据?如果没有,怎么办?

h5 = h5py.File('myfile.h5', 'r')
mydata = h5.get('matirx') # are all data loaded into memory by using h5.get?
part_of_mydata= mydata[1000:11000,:]
size_data =  mydata.shape 

谢谢。

【问题讨论】:

    标签: python hdf5 h5py


    【解决方案1】:

    get(或索引)获取对文件中数据集的引用,但不加载任何数据。

    In [789]: list(f.keys())
    Out[789]: ['dset', 'dset1', 'vset']
    In [790]: d=f['dset1']
    In [791]: d
    Out[791]: <HDF5 dataset "dset1": shape (2, 3, 10), type "<f8">
    In [792]: d.shape         # shape of dataset
    Out[792]: (2, 3, 10)
    In [793]: arr=d[:,:,:5]    # indexing the set fetches part of the data
    In [794]: arr.shape
    Out[794]: (2, 3, 5)
    In [795]: type(d)
    Out[795]: h5py._hl.dataset.Dataset
    In [796]: type(arr)
    Out[796]: numpy.ndarray
    

    d 数据集类似于数组,但实际上不是numpy 数组。

    获取整个数据集:

    In [798]: arr = d[:]
    In [799]: type(arr)
    Out[799]: numpy.ndarray
    

    究竟如何读取文件以获取您的切片取决于切片、数据布局、分块和其他通常不受您控制的事情,您不必担心。

    另请注意,在读取一个数据集时,我不会加载其他数据集。这同样适用于组。

    http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多