python讀取.mat文件的數(shù)據(jù)及實例代碼
首先導入scipy的包 from scipy.io import loadmat
然后讀取 m = loadmat("F:/__identity/activity/論文/data/D001.mat")
注意這里m是一個dict數(shù)據(jù)結(jié)構(gòu)
>>> 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])
這個數(shù)據(jù)類型真是醉了:
>>> 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
總結(jié)
以上所述是小編給大家介紹的python讀取.mat文件的數(shù)據(jù) ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細介紹了TensorFlow實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Python異常對象Exception基礎(chǔ)類異常捕捉
這篇文章主要為大家介紹了Python異常對象異常捕捉及Exception基礎(chǔ)類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Python?pycharm提交代碼遇到?jīng)_突解決方法
這篇文章主要介紹了Python?pycharm提交代碼遇到?jīng)_突解決方法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08python調(diào)用系統(tǒng)ffmpeg實現(xiàn)視頻截圖、http發(fā)送
這篇文章主要為大家詳細介紹了python調(diào)用系統(tǒng)ffmpeg實現(xiàn)視頻截圖、http發(fā)送,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03