HDF(Hierarchical Data Format)指一种为存储和处理大容量科学数据设计的文件格式及相应库文件。HDF 最先由美国国家超级计算应用中心 NCSA 开发,目前在非盈利组织 HDF 小组维护下继续发展。当前流行的版本是 HDF5。HDF5 拥有一系列的优异特性,使其特别适合进行大量科学数据的存储和操做,如它支持很是多的数据类型,灵活,通用,跨平台,可扩展,高效的 I/O 性能,支持几乎无限量(高达 EB)的单文件存储等。html
HDF5文件层次化的存储两类对象:python
这两类对象均可以设置各类属性,属性用于描述group和dataset的一些特色。一个HDF5文件从一个命名为“/”的group开始,一个HDF5文件只有一个根group。数组
用 h5py 操做 HDF5 文件,咱们能够像使用目录同样使用 group,像使用 numpy 数组同样使用 dataset,像使用字典同样使用属性,很是方便和易用。dom
class File(name, mode=None, driver=None, libver=None, userblock_size=None, **kwds)
打开或建立一个 HDF5 文件,name 为文件名字符串,mode 为打开文件的模式,driver 能够指定一种驱动方式,如需进行并行 HDF5 操做,可设置为 'mpio',libver 能够指定使用的兼容版本,默认为 'earliest',也能够指定为 'latest',userblock_size 以字节为单位指定一个在文件开头称做 user block 的数据块,通常不须要设置。返回所打开文件的句柄。ide
mode | 说明 |
---|---|
r | 只读,文件必须存在 |
r+ | 读写,文件必须存在 |
w | 建立新文件写,已经存在的文件会被覆盖掉 |
w- | / x 建立新文件写,文件若是已经存在则出错 |
a | 打开已经存在的文件进行读写,若是不存在则建立一个新文件读写,此为默认的 mode |
create_group(self, name, track_order=False)
建立一个新的 group。以相似目录路径的形式指明所建立 group 的名字 name,若是 track_order 为 True,则会跟踪在当前 group 下的 group 和 dataset 建立的前后顺序。该方法能够在打开的文件句柄(至关于 "/" group)或者一个存在的 group 对象上调用,此时 name 的相对路径就是相对于此 group 的。性能
create_dataset(self, name, shape=None, dtype=None, data=None, **kwds)
建立一个新的 dataset。以相似文件路径的形式指明所建立 dataset 的名字 name,shape 以一个 tuple 或 list 的形式指明建立 dataset 的 shape,用 "()" 指明标量数据的 shape,dtype 指明所建立 dataset 的数据类型,能够为 numpy dtype 或者一个代表数据类型的字符串,data 指明存储到所建立的 dataset 中的数据。若是 data 为 None,则会建立一个空的 dataset,此时 shape 和 dtype 必须设置;若是 data 不为 None,则 shape 和 dtype 能够不设置而使用 data 的 shape 和 dtype,可是若是设置的话,必须与 data 的 shape 和 dtype 兼容。ui
打开的文件句柄(至关于 "/" group),group 和 dataset 上均可以建立 attribute,以相似于字典的操做方式建立和读取 attribute。设计
示例代码一:code
import h5py import numpy as np X = np.random.rand(1, 10, 4).astype('float32') y = np.random.rand(1, 10, 5).astype('float32') h5f = h5py.File('data.h5', 'w') # 以写模式打开文件 h5f.create_dataset('X_train', data=X) # 添加数据集 h5f.create_dataset('y_train', data=y) # 添加数据集 h5f.close() h5f = h5py.File('data.h5', 'r') # 以读模式打开文件 X = h5f['X_train'] # 经过下标方式获取数据集 Y = h5f['y_train'] h5f.close()
示例代码二:orm
import os import h5py import numpy as np file_name = 'test.hdf5' # create a new HDF5 file f = h5py.File(file_name) # create a new group f.create_group('/grp1') # or f.create_group('grp1') # create a nother group inside grp1 f.create_group('/grp1/grp2') # or f.create_group('grp1/grp2') # create a dataset in group "/" data = np.arange(6).reshape(2, 3) f.create_dataset('dset1', data=data) # or f.create_dataset('/dset1', data=data) # create another dataset in group /grp1 f.create_dataset('grp1/dset2', data=data) # or f.create_dataset('/grp1/dset2', data=data) # create an attribute of "/" f.attrs['a'] = 1 # or f.attrs['/a'] = 1 # create an attribute of group "/grp1" f['grp1'].attrs['b'] = 'xyz' # create an attribute of dataset "/grp1/dset2" f['grp1/dset2'].attrs['c'] = np.array([1, 2]) # close file f.close() # open the existing test.hdf5 for read only f = h5py.File(file_name, 'r') # read dataset /dset1 print('/dset1 = %s' % f['dset1'][:]) # read dataset /grp1/dset2 print('/grp1/dset2 = %s' % f['/grp1/dset2'][:]) # get attributes print(f.attrs['a']) print(f['grp1'].attrs['b']) print(f['grp1/dset2'].attrs['c']) # remove the created file os.remove(file_name)
示例代码三:
import h5py import numpy as np file_name = 'test.hdf5' f = h5py.File(file_name, mode='w') data = np.array([1, 2, 3]) f['/one'] = data f.attrs['one'] = 'haha' print(f.attrs.keys()) print(f['one']) print(f.attrs['one'])