如何使用h5py导入.mat-v7.3文件 [英] how to import .mat-v7.3 file using h5py

查看:137
本文介绍了如何使用h5py导入.mat-v7.3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有.mat文件,其中包含3个矩阵A,B,C.

I have .mat file which have 3 matrixes A, B, C.

实际上,我使用scipy.io如下导入了该mat文件.

Actually I used scipy.io to import this mat file as below.

data = sio.loadmat('/data.mat')
A = data['A']
B = data['B']
C = data['C']

但是,无法使用这种方式导入v7.3文件. 因此,我尝试使用h5py进行导入,但是我不知道如何使用h5py. 我的代码如下.

But, v7.3 file cannot import using this way. So, I tried to import using h5py but I don't know how to use h5py. My code is as below.

f = h5py.File('/data.mat', 'r')
A = f.get('/A')
A = np.array('A')

哪一部分是错误的? 谢谢!

Which part is wrong? Thank you!

推荐答案

在八度音阶中

>> A = [1,2,3;4,5,6];
>> B = [1,2,3,4];
>> save -hdf5 abc.h5 A B

在Ipython中

In [138]: import h5py
In [139]: f = h5py.File('abc.h5')
In [140]: list(f.keys())
Out[140]: ['A', 'B']
In [141]: list(f['A'].keys())
Out[141]: ['type', 'value']
In [142]: f['A']['value']
Out[142]: <HDF5 dataset "value": shape (3, 2), type "<f8">
In [143]: A = f['A']['value'][:]
In [144]: A
Out[144]: 
array([[ 1.,  4.],
       [ 2.,  5.],
       [ 3.,  6.]])

另请参阅侧边栏中的链接.

See also links in the sidebar.

基本上,这是找到所需的数据集,然后按照

Basically it's a matter of finding the desired dataset, and then loading it as described in http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data

https://pypi.python.org/pypi/hdf5storage/0.1.14 -此程序包具有MATLAB MAT v7.3 file support.我还没有用过.

https://pypi.python.org/pypi/hdf5storage/0.1.14 - this package has MATLAB MAT v7.3 file support. I haven't used it yet.

In [550]: import hdf5storage
In [560]: bar = hdf5storage.read(filename='abc.h5')
In [561]: bar
Out[561]: 
array([ ([(b'matrix', [[ 1.,  4.], [ 2.,  5.], [ 3.,  6.]])], [(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])])],
      dtype=[('A', [('type', 'S7'), ('value', '<f8', (3, 2))], (1,)), ('B', [('type', 'S7'), ('value', '<f8', (4, 1))], (1,))])

因此,文件已作为具有形状(1,)和2个字段"A"和"B"(2个变量名称)的结构化数组加载.每个字段都有一个类型"和值"字段.

So the file has been loaded as a structured array, with shape (1,) and 2 fields, 'A' and 'B' (the 2 variable names). Each in turn has a 'type' and 'value' field.

In [565]: bar['A']['value']
Out[565]: 
array([[[[ 1.,  4.],
         [ 2.,  5.],
         [ 3.,  6.]]]])

或使用其loadmat:

In [570]: out = hdf5storage.loadmat('abc.h5',appendmat=False)
In [571]: out
Out[571]: 
{'A': array([(b'matrix', [[ 1.,  4.], [ 2.,  5.], [ 3.,  6.]])],
       dtype=[('type', 'S7'), ('value', '<f8', (3, 2))]),
 'B': array([(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])],
       dtype=[('type', 'S7'), ('value', '<f8', (4, 1))])}

out是字典:

In [572]: out['B']['value']
Out[572]: 
array([[[ 1.],
        [ 2.],
        [ 3.],
        [ 4.]]])

对于读取简单的MATLAB文件,这并没有增加太多.它可能会添加更多的单元格或结构.但是对于编写与MATLAB兼容的文件,它应该是一个很大的帮助(尽管编写一个文件可能会坚持使用scipy.io.savemat).

For reading a simple MATLAB file this doesn't add much. It may add more with cells or structs. But for writing a MATLAB compatible file it should be a big help (though for writing one could stick with scipy.io.savemat).

这篇关于如何使用h5py导入.mat-v7.3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆