本文介绍了将 Numpy 结构数组保存到 *.mat 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 numpy.loadtext 从 CSV 数据文件生成结构化的 Numpy 数组,我想将其保存到 MAT 文件中,供更熟悉 MATLAB 而非 Python 的同事使用.>

示例:

将 numpy 导入为 np导入 scipy.iomydata = np.array([(1, 1.0), (2, 2.0)], dtype=[('foo', 'i'), ('bar', 'f')])scipy.io.savemat('test.mat', mydata)

当我尝试在这个数组上使用 scipy.io.savemat 时,抛出以下错误:

回溯(最近一次调用最后一次):文件C:/Project Data/General Python/test.py",第 6 行,在 <module> 中scipy.io.savemat('test.mat', mydata)文件C:python35libsite-packagesscipyiomatlabmio.py",第 210 行,在 savematMW.put_variables(mdict)文件C:python35libsite-packagesscipyiomatlabmio5.py",第 831 行,在 put_variables对于名称,mdict.items() 中的 var:AttributeError: 'numpy.ndarray' 对象没有属性 'items'

我是 Python 新手(充其量),但我假设这是因为 savemat 设置为处理 dicts 并且 Numpy 的结构化数组的结构不兼容.

我可以通过将我的数据拉入字典来解决这个错误:

tmp = {}对于 mydata.dtype.names 中的 varname:tmp[varname] = mydata[varname]scipy.io.savemat('test.mat', tmp)

可以很好地加载到 MATLAB 中:

>>mydata = load('test.mat')我的数据 =富:[1 2]酒吧:[1 2]

但这似乎是一种非常低效的方法,因为我正在复制内存中的数据.有没有更聪明的方法来实现这一点?

解决方案

你可以做 scipy.io.savemat('test.mat', {'mydata': mydata}).>

这会在文件中创建一个结构体 mydata,其中包含字段 foobar.

或者,您可以将循环打包在字典理解中:

tmp = {varname: mydata[varname] for varname in mydata.dtype.names}

我不认为创建临时字典会复制内存中的数据,因为 Python 通常只存储引用,尤其是 numpy 会尽可能在原始数据中创建视图.

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.

Sample case:

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)

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:python35libsite-packagesscipyiomatlabmio.py", line 210, in savemat
    MW.put_variables(mdict)
  File "C:python35libsite-packagesscipyiomatlabmio5.py", line 831, in put_variables
    for name, var in mdict.items():
AttributeError: 'numpy.ndarray' object has no attribute 'items'

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)

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?

解决方案

You can do scipy.io.savemat('test.mat', {'mydata': mydata}).

This creates a struct mydata with fields foo and bar in the file.

Alternatively, you can pack your loop in a dict comprehension:

tmp = {varname: mydata[varname] for varname in mydata.dtype.names}

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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:36