Python读取mat文件

  参考资料:

  https://www.cnblogs.com/anyview/p/5064174.html

  在处理数据的时候可能会遇到原始数据储存在.mat文件的情况,mat文件当然就是matlab中保存的工作区文件啦,里面可能会有一个或者多个矩阵。给你一个mat文件你又想用python处理,该怎么办呢?

  在网上找到了一个简单的解决方案,下面连带效果图一起贴出来:

import scipy.io as scio

train_imgs_path = 'train_imgs.mat'
train_imgs = scio.loadmat(train_imgs_path)
print(train_imgs)
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Fri Apr  3 17:38:47 2020',
'__version__': '1.0', 
'__globals__': [], 
'train_imgs': array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=int32)
}

  显然返回的是一个字典对象,在其中取出我们需要的数组即可。不过我很诧异的是这个数组的数据类型居然是ndarray,不管那么多了,好用就行。

原文地址:https://www.cnblogs.com/chester-cs/p/12683491.html