如何将Mat文件转换为numpy数组 [英] How to convert mat file to numpy array
问题描述
我想将大小为600 x 600的mat文件转换为numpy数组,并且出现此错误"float()参数必须是字符串或数字,而不是'dict'".我想知道如何解决它.
I want to convert a mat file with size 600 by 600 to numpy array and I got this error "float() argument must be a string or a number, not 'dict'" I am wondering how can I fix it.
import numpy as np
import scipy.io as sio
test = sio.loadmat('Y7.mat')
data=np.zeros((600,600))
data[:,:]=test
推荐答案
In [240]: from scipy.io import loadmat
使用过去的SO问题中提供的测试垫文件:
Using a test mat file that I have from past SO questions:
In [241]: loadmat('test.mat')
Out[241]:
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sat Mar 21 20:09:30 2020',
'__version__': '1.0',
'__globals__': [],
'x': array([[ 0, 3, 6, 9],
[ 1, 4, 7, 10],
[ 2, 5, 8, 11]])}
loadmat
给了我一个字典(请参阅{}?).碰巧只有一个数组,键为"x".因此,我仅使用标准字典索引对其进行访问:
loadmat
has given me a dictionary (see the {}?). This happens to have one array, with key 'x'. So I just access it with standard dictionary indexing:
In [242]: _['x']
Out[242]:
array([[ 0, 3, 6, 9],
[ 1, 4, 7, 10],
[ 2, 5, 8, 11]])
x
是保存此文件的MATLAB会话中的变量.
x
was a variable in the MATLAB session that saved this file.
我对您文件中的内容一无所知. print(list(data(key.keys()))
可用于查看那些键/变量名.
I don't know anything about what's on your file. print(list(data.keys()))
can be used to see those keys/variable names.
我试图让您查看 loadmat
文档,并看到它:
I tried to get you to look at the loadmat
docs, and see that it:
Returns
mat_dictdict
dictionary with variable names as keys, and loaded matrices as values.
这篇关于如何将Mat文件转换为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!