当数据太大(好像是 > 2G?),scipy.io.savemat 会报错,考虑换用 h5py。这种格式 matlab 也可以读,见 [4]。
Code
import numpy as np
import h5py
a = np.arange(12).reshape(3, 4)
print("a:", a)
# 保存
with h5py.File("test.h5", "w") as f:
f.create_dataset("images", data=a)
# 读取
with h5py.File("test.h5", "r") as f:
for k in f.keys():
print(k)
# 此处必加 `[()]` 之类的 slicing 操作
# 不然离开这个 `with` 的域,后面的 `print` 会报错
# 解释见 [1]
b = f["images"][()]
print("b:", b)
print(type(b))
print(b.shape)
print(b.dtype)
References
- How to use HDF5 files in Python
- python读取.mat数据之scipy.io&h5py
- Python数据存储之 h5py
- matlab读.h5文件