问题
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
回答1:
In [240]: from scipy.io import loadmat
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
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
was a variable in the MATLAB session that saved this file.
I don't know anything about what's on your file. print(list(data.keys()))
can be used to see those keys/variable names.
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.
来源:https://stackoverflow.com/questions/62274746/how-to-convert-mat-file-to-numpy-array