模型复现之HDF5文件

GitHub上下载的代码,涉及到的数据都是以.h5为后缀的,那么这是什么类型的文件呢?

可以找到,代码中都引入了这个包h5py,接下来一起看看这种文件怎么读取吧!

An HDF5 file is a container for two kinds of objects: datasets, which are array-like collections of data, andgroups, which are folder-like containers that hold datasets and other groups.

Groups work like dictionaries, and datasets work like NumPy arrays

拿到别人给你的文件mytestfile.hdf5,如何读取呢?看下面代码:

# 导入包
import h5py
# 读取文件,r表示只读
f = h5py.File('mytestfile.hdf5', 'r')
# 查看文件keys
list(f.keys()) # ['mydataset']
# Remember h5py.File acts like a Python dictionary, thus we can check the keys
# 将file中key对应的dataset提取出来
data = f['mydataset']
# 这个dataset是HDF5 dataset类型,有dtype和shape可以查看类型和大小
data.shape
data.dtype
# 同样支持array的切片操作
data[...] = np.arange(100)
data[0:100:10] # array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

那么,你自己可以创建一个HDF5吗?

import h5py
# 创建一个file,w表示写
f = h5py.File("myh5py.hdf5","w")
# 创建一个dataset
dataset = f.create_dataset("d1",(100,),dtype=
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值