python读取.mat文件的数据及实例代码

yipeiwu_com6年前Python基础

首先导入scipy的包 from scipy.io import loadmat

然后读取 m = loadmat("F:/__identity/activity/论文/data/D001.mat")

注意这里m是一个dict数据结构

>>> m
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Mon Aug 15 22:16:43 2011', '__globals__': [], 'labels': array([[1],
  [3],
  [4],
  ...,
  [4],
  [3],
  [4]], dtype=uint8), 'data': array([[ 1. , 0.35 , 0.265 , ..., 0.0995, 0.0485, 0.07 ],
  [ 2. , 0.53 , 0.42 , ..., 0.2565, 0.1415, 0.21 ],
  [ 1. , 0.44 , 0.365 , ..., 0.2155, 0.114 , 0.155 ],
  ...,
  [ 1. , 0.59 , 0.44 , ..., 0.439 , 0.2145, 0.2605],
  [ 1. , 0.6 , 0.475 , ..., 0.5255, 0.2875, 0.308 ],
  [ 2. , 0.625 , 0.485 , ..., 0.531 , 0.261 , 0.296 ]]), '__version__': '1.0'}

>>> m.keys()
dict_keys(['__header__', '__globals__', 'labels', 'data', '__version__'])

>>> m["labels"]
array([[1],
  [3],
  [4],
  ...,
  [4],
  [3],
  [4]], dtype=uint8)

>>> m["data"]
array([[ 1. , 0.35 , 0.265 , ..., 0.0995, 0.0485, 0.07 ],
  [ 2. , 0.53 , 0.42 , ..., 0.2565, 0.1415, 0.21 ],
  [ 1. , 0.44 , 0.365 , ..., 0.2155, 0.114 , 0.155 ],
  ...,
  [ 1. , 0.59 , 0.44 , ..., 0.439 , 0.2145, 0.2605],
  [ 1. , 0.6 , 0.475 , ..., 0.5255, 0.2875, 0.308 ],
  [ 2. , 0.625 , 0.485 , ..., 0.531 , 0.261 , 0.296 ]])

有点不太懂这个“uint8”

>>> m["labels"][0]
array([1], dtype=uint8)
>>> m["labels"][0][0]
1
>>> m["labels"][0][0] + 1
2
>>> m["labels"][0].as_type("int")
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'as_type' # 注意时astype不是as_type
>>> m["labels"][0].dtype
dtype('uint8')
>>> m["labels"][0].astype("int")
array([1])

这个数据类型真是醉了:

>>> type(m["labels"][0][0] + 1)
<class 'numpy.int32'>

如果要把它变成dataframe,导入pandas后

>>> df = pd.DataFrame(m["data"])
>>> df.head()
  0  1  2  3  4  5  6  7
0 1.0 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.070
1 2.0 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.210
2 1.0 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.155
3 3.0 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.055
4 3.0 0.425 0.300 0.095 0.3515 0.1410 0.0775 0.120

总结

以上所述是小编给大家介绍的python读取.mat文件的数据 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

python list数据等间隔抽取并新建list存储的例子

原始数据如下: ['e3cd', 'e547', 'e63d', '0ffd', 'e39b', 'e539', 'e5be', '0dd2', 'e3d6', 'e52e', 'e...

跟老齐学Python之Python安装

任何高级语言都是需要一个自己的编程环境的,这就好比写字一样,需要有纸和笔,在计算机上写东西,也需要有文字处理软件,比如各种名称的OFFICE。笔和纸以及office软件,就是写东西的硬件...

pyqt5对用qt designer设计的窗体实现弹出子窗口的示例

1. 用qt designer编写主窗体,窗体类型是MainWindow,空白窗口上一个按钮。并转换成mainWindow.py # -*- coding: utf-8 -*- #...

python发送告警邮件脚本

python发送告警邮件脚本

python脚本为敏捷开发脚本,在zabbix监控也起到重要作用,以下是使用python脚本发送告警邮件配置方法。 脚本如下: #!/usr/bin/python #coding:u...

详解python进行mp3格式判断

项目中使用mp3格式进行音效播放,遇到一个mp3文件在程序中死活播不出声音,最后发现它是wav格式的文件,却以mp3结尾。要对资源进行mp3格式判断,那么如何判断呢,用.mp3后缀肯定不...