Python读取.mat文件的数据

2020-08-11  本文已影响0人  羋学僧

首先导入scipy的包

from scipy.io import loadmat

然后读取

 m = loadmat("B0006_404.mat") 
注意这里m是一个dict字典数据结构

查看key

m.keys()

数据展示

>>> 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 ]])

>>> 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])
>>> 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
上一篇下一篇

猜你喜欢

热点阅读