在h5py文件上传中,有什么区别np.数组(文件[key][:])和np.数组(文件[键])

2024-04-18 12:59:28 发布

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

我目前正在玩h5py文件提取。当我运行下面的脚本时,它似乎输出了相同的结果。有人知道区别吗?你知道吗

train_dataset = h5py.File('datasets/train_happy.h5', "r")
train_set_x_orig1 = np.array(train_dataset["train_set_x"][:])

train_set_x_orig2 = np.array(train_dataset["train_set_y"]) 

感谢任何人提供的意见!你知道吗


Tags: 文件脚本nptrainarraydatasetfiledatasets
1条回答
网友
1楼 · 发布于 2024-04-18 12:59:28

还有其他问题的样本文件

In [183]: f = h5py.File('temp.h5','r')
In [184]: list(f.keys())
Out[184]: ['db1', 'db2', 'db3', 'db4', 'temp']

简单地请求键就会返回一个Dataset(类似于字典的操作)

In [185]: x = f['db1']
In [186]: type(x)
Out[186]: h5py._hl.dataset.Dataset
In [187]: x
Out[187]: <HDF5 dataset "db1": shape (5,), type "|V4">

添加[:](或其他索引)就足以将数据加载到数组中:

In [188]: y = f['db1'][:]
In [189]: type(y)
Out[189]: numpy.ndarray
In [190]: y
Out[190]: array([('a',), ('ab',), ('',), ('',), ('',)], dtype=[('str', 'O')])

不需要进一步的np.array包装。你知道吗

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

value属性也起作用(我不确定这是在哪里记录的):

In [191]: x.value
Out[191]: array([('a',), ('ab',), ('',), ('',), ('',)], dtype=[('str', 'O')])

array包装器工作:

In [192]: np.array(x)
Out[192]: array([('a',), ('ab',), ('',), ('',), ('',)], dtype=[('str', 'O')])

一个快速的timeits集没有显示任何区别。你知道吗

但在发行说明中:

http://docs.h5py.org/en/latest/whatsnew/2.1.html#dataset-value-property-is-now-deprecated

The property Dataset.value, which dates back to h5py 1.0, is deprecated and will be removed in a later release. This property dumps the entire dataset into a NumPy array. Code using .value should be updated to use NumPy indexing, using mydataset[...] or mydataset[()] as appropriate.

相关问题 更多 >