我使用以下代码将 MATLAB 文件加载到 Python 中
import h5py
import numpy as np
filepath = 'file.mat'
arrays = {}
f = h5py.File(filepath)
for k, v in f.items():
arrays[k] = np.array(v)
a=arrays['data']
a.shape
在 MATLAB 中我的数据具有以下大小:
>> size(dataset)
ans =
64 50 21 2
在Python中
>>> a.shape
(2, 21, 50, 64)
为什么我的尺寸向后排列以及如何解决这个问题?
numpy.moveaxis()
可以排列你的轴:
import numpy as np
tmp = np.zeros((2, 21, 50, 64))
tmp2 = np.moveaxis(tmp, [0, 1, 2, 3], [3, 2, 1, 0])
tmp2.shape
Out[5]: (64, 50, 21, 2)