我正试图在Python中从hdf5文件中读取数据。使用h5py
,我可以读取hdf5文件,但我无法弄清如何访问文件中的数据。
我的代码
import h5py
import numpy as np
f1 = h5py.File(file_name,'r+')
这样可以工作,文件也已经被读取。但是我该如何访问文件对象f1
中的数据?
import h5py
filename = "file.hdf5"
with h5py.File(filename, "r") as f:
# Print all root level object names (aka keys)
# these can be group or dataset names
print("Keys: %s" % f.keys())
# get first object name/key; may or may NOT be a group
a_group_key = list(f.keys())[0]
# get the object type for a_group_key: usually group or dataset
print(type(f[a_group_key]))
# If a_group_key is a group name,
# this gets the object names in the group and returns as a list
data = list(f[a_group_key])
# If a_group_key is a dataset name,
# this gets the dataset values and returns as a list
data = list(f[a_group_key])
# preferred methods to get dataset values:
ds_obj = f[a_group_key] # returns as a h5py dataset object
ds_arr = f[a_group_key][()] # returns as a numpy array
import h5py
# Create random data
import numpy as np
data_matrix = np.random.uniform(-1, 1, size=(10, 3))
# Write data to HDF5
with h5py.File("file.hdf5", "w") as data_file:
data_file.create_dataset("dataset_name", data=data_matrix)
查看h5py文档以获取更多信息。
对于您的应用程序,以下可能很重要:
另请参见:数据序列化格式比较
如果您更希望找到一种创建配置文件的方法,您可能想阅读我的短文Python中的配置文件
f[key].value
。 - erickrfh5py
版本 2.1 开始:"属性 Dataset.value
早在 h5py 1.0 版本就已被弃用,并将在以后的版本中删除。该属性将整个数据集转储到 NumPy 数组中。使用 .value
的代码应更新为使用 NumPy 索引,适当地使用 mydataset[...]
或 mydataset[()]
。" - honey_badger读取文件
import h5py
f = h5py.File(file_name, mode)
通过打印现有的HDF5组来研究文件的结构
for key in f.keys():
print(key) #Names of the root level object names in HDF5 file - can be groups or datasets.
print(type(f[key])) # get the object type: usually group or dataset
提取数据
#Get the HDF5 group; key needs to be a group name from above
group = f[key]
#Checkout what keys are inside that group.
for key in group.keys():
print(key)
# This assumes group[some_key_inside_the_group] is a dataset,
# and returns a np.array:
data = group[some_key_inside_the_group][()]
#Do whatever you want with data
#After you are done
f.close()
data.visit(print)
。 - Hiteshdata.close()
。 - anilbeydata = group[some_key_inside_the_group][()]
。 - Bersan你可以使用Pandas。
import pandas as pd
pd.read_hdf(filename,key)
这是我刚写的一个简单函数,它可以读取由Keras中的save_weights函数生成的.hdf5文件,并返回一个包含层名称和权重的字典:
def read_hdf5(path):
weights = {}
keys = []
with h5py.File(path, 'r') as f: # open file
f.visit(keys.append) # append all keys to list
for key in keys:
if ':' in key: # contains data if ':' in key
print(f[key].name)
weights[f[key].name] = f[key].value
return weights
这里提供的代码与it技术有关,虽然我没有进行全面测试,但可以胜任我的工作。
> import numpy as np
> myarray = np.fromfile('file.hdf5', dtype=float)
> print(myarray)
import h5py
f1 = h5py.File('data_1.h5', 'r')
list(f1.keys())
X1 = f1['x']
y1=f1['y']
df1= np.array(X1.value)
dfy1= np.array(y1.value)
print (df1.shape)
print (dfy1.shape)
将数据集值读入numpy数组的首选方法:
import h5py
# use Python file context manager:
with h5py.File('data_1.h5', 'r') as f1:
print(list(f1.keys())) # print list of root level objects
# following assumes 'x' and 'y' are dataset objects
ds_x1 = f1['x'] # returns h5py dataset object for 'x'
ds_y1 = f1['y'] # returns h5py dataset object for 'y'
arr_x1 = f1['x'][()] # returns np.array for 'x'
arr_y1 = f1['y'][()] # returns np.array for 'y'
arr_x1 = ds_x1[()] # uses dataset object to get np.array for 'x'
arr_y1 = ds_y1[()] # uses dataset object to get np.array for 'y'
print (arr_x1.shape)
print (arr_y1.shape)
import h5py
file = h5py.File('filename.h5', 'r')
xdata = file.get('xdata')
xdata= np.array(xdata)
如果您的文件在不同的目录中,您可以在'filename.h5'
前面添加路径。
from keras.models import load_model
h= load_model('FILE_NAME.h5')
使用这个,对我来说很好用。
weights = {}
keys = []
with h5py.File("path.h5", 'r') as f:
f.visit(keys.append)
for key in keys:
if ':' in key:
print(f[key].name)
weights[f[key].name] = f[key][()]
return weights
print(read_hdf5())
如果您正在使用h5py<='2.9.0',那么您可以使用以下代码:
weights = {}
keys = []
with h5py.File("path.h5", 'r') as f:
f.visit(keys.append)
for key in keys:
if ':' in key:
print(f[key].name)
weights[f[key].name] = f[key].value
return weights
print(read_hdf5())
f.create_dataset
然后你就可以读取数据了。这在文档中有详细解释。
hdf5
文件和一个hdf
文件有什么不同?我有一些hdf
文件(它们是几个图像波段),但我无法弄清如何打开它们。 - mikey