将Numpy结构数组保存到* .mat文件 [英] Saving Numpy Structure Array to *.mat file
问题描述
我正在使用numpy.loadtext
从CSV数据文件生成结构化的Numpy数组,我想将其保存到MAT文件中,以供比Python更熟悉MATLAB的同事使用.
I am using numpy.loadtext
to generate a structured Numpy array from a CSV data file that I would like to save to a MAT file for colleagues who are more familiar with MATLAB than Python.
示例案例:
import numpy as np
import scipy.io
mydata = np.array([(1, 1.0), (2, 2.0)], dtype=[('foo', 'i'), ('bar', 'f')])
scipy.io.savemat('test.mat', mydata)
当我尝试在此数组上使用scipy.io.savemat
时,将引发以下错误:
When I attempt to use scipy.io.savemat
on this array, the following error is thrown:
Traceback (most recent call last):
File "C:/Project Data/General Python/test.py", line 6, in <module>
scipy.io.savemat('test.mat', mydata)
File "C:\python35\lib\site-packages\scipy\io\matlab\mio.py", line 210, in savemat
MW.put_variables(mdict)
File "C:\python35\lib\site-packages\scipy\io\matlab\mio5.py", line 831, in put_variables
for name, var in mdict.items():
AttributeError: 'numpy.ndarray' object has no attribute 'items'
我是一名Python新手(充其量),但是我认为这是因为savemat
已设置为处理字典,并且Numpy的结构化数组的结构不兼容.
I'm a Python novice (at best), but I'm assuming this is because savemat
is set up to handle dicts and the structure of Numpy's structured arrays is not compatible.
我可以通过将数据提取到字典中来解决此错误:
I can get around this error by pulling my data into a dict:
tmp = {}
for varname in mydata.dtype.names:
tmp[varname] = mydata[varname]
scipy.io.savemat('test.mat', tmp)
哪些可以很好地加载到MATLAB中?
Which loads into MATLAB fine:
>> mydata = load('test.mat')
mydata =
foo: [1 2]
bar: [1 2]
但是,这似乎是一种效率很低的方法,因为我正在复制内存中的数据.有没有更聪明的方法可以做到这一点?
But this seems like a very inefficient method since I'm duplicating the data in memory. Is there a smarter way to accomplish this?
推荐答案
您可以执行scipy.io.savemat('test.mat', {'mydata': mydata})
.
这将在文件中创建具有字段foo
和bar
的结构mydata
.
This creates a struct mydata
with fields foo
and bar
in the file.
或者,您可以将循环打包到dict理解中:
Alternatively, you can pack your loop in a dict comprehension:
tmp = {varname: mydata[varname] for varname in mydata.dtype.names}
我不认为创建临时字典会在内存中复制数据,因为Python通常只存储引用,尤其是numpy会尝试尽可能地在原始数据中创建视图.
I don't think creating a temprorary dictionary duplicates data in memory, because Python generally only stores references, and numpy in particular tries to create views into the original data whenever possible.
这篇关于将Numpy结构数组保存到* .mat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!