使用Scipy将字典从Python保存到Matlab [英] Saving dictionaries from Python to Matlab with Scipy

查看:93
本文介绍了使用Scipy将字典从Python保存到Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些问题,无法将生成的整洁数据保存到.mat文件中.我以为使用Scipy会更简单,但是看来我出了点问题.

I'm finding some problems to save my neat generated data into .mat files. I thought it was more straightforward with Scipy, but it seems I'm getting something wrong.

这是我要保存的数据的示例:

This is an example of data I want to save:

out = {'features': array([[  5.00088905e+01,   1.51847522e+01,   4.93513862e+01,
          3.76548415e+00,  -3.96946513e+01,  -2.11885850e+01,
          9.85304035e+00,  -6.30005764e+00,   1.19987435e+01,
          3.89762536e+00,  -1.31554755e+00,  -1.66890836e+01,
          4.75289017e-02,   3.65829480e-01,  -4.77872832e-01,
          1.13641908e+00,  -1.08742775e-01,  -2.42751445e-01,
         -1.13054913e-01,   3.39011561e-01,   1.37158960e-01,
         -2.80760116e-01,  -4.15187861e-01,   9.85433526e-02,
         -8.66144928e-02,   9.18260870e-03,  -7.38139130e-01,
          8.04136232e-01,   2.31623188e-02,  -7.88927536e-02,
         -2.17779710e-01,   2.85428986e-01,  -8.16231884e-02,
          1.79710145e-03,  -3.47710145e-01,  -9.84115942e-02,
          3.96077031e+00,   3.29914828e+01,   2.60086805e+01,
          2.44418378e+01,   2.01712577e+01,   1.56827627e+01,
          1.59131122e+01,   1.84134126e+01,   1.63149310e+01,
          1.35579058e+01,   1.15772911e+01,   1.82263123e+01,
          3.96077031e+00,   3.29914828e+01,   2.60086805e+01,
          2.44418378e+01,   2.01712577e+01,   1.56827627e+01,
          1.59131122e+01,   1.84134126e+01,   1.63149310e+01,
          1.35579058e+01,   1.15772911e+01,   1.82263123e+01,
          3.96077031e+00,   3.29914828e+01,   2.60086805e+01,
          2.44418378e+01,   2.01712577e+01,   1.56827627e+01,
          1.59131122e+01,   1.84134126e+01,   1.63149310e+01,
          1.35579058e+01,   1.15772911e+01,   1.82263123e+01]]), 'tags': [['rock', 'metal']]}

它是矩阵的单行,可以与标签列表(长度可变)相关联.

It's a single row of a matrix that can be associated with a list of tags (variable in lengths).

这个想法是要创建一个.mat文件,其中包含矩阵和列表的单元格数组.当我这样做时:

The idea was to have a .mat file with the matrix and a cell array of lists. When I go for this:

scipy.io.savemat('./test.mat',out)

在Matlab中获取标签的结果各不相同.对于上面的示例,我有一个1x2x5的字符矩阵

results in Matlab for tags vary. For the example above I have a 1x2x5 char matrix

val(:,:,1) =    rm    
val(:,:,2) =    oe    
val(:,:,3) =    ct    
val(:,:,4) =    ka    
val(:,:,5) =     l

如果我尝试使用矩阵而不是单行向量,则会得到一个单元格数组,其中每行都有一个单元格,但是列表被合并,特定行的单元格为: rmoectkal .

If I try for matrix instead than single row vectors, I get a cell array with a cell for every row, but the list is merged and the cell for the specific row would be: rmoectkal.

我尝试举一个例子来解释:

Il try to explain with an example:

>>> genre_tags_matrix = np.array(genre_tags, dtype=np.object) 
>>> print(genre_tags_matrix) 
[['classical', 'pop'] ['classical'] ['classical'] ['classical']]
>>> out = {'tags' : genre_tags_matrix}
>>> scipy.io.savemat('./test.mat',out)

这是我在Matlab中看到的:

This is what I see in Matlab:

到底是怎么回事?有解决方法吗?

What exactly is going on? Is there a workaround for this?

推荐答案

问题是MATLAB和Octave中的字符串实际上只是一个字符数组,因此以下语句实际上是3D图像.数组

The issue is that a string in MATLAB and Octave is really just an array of characters, so the following statement is actually a 3D array

[['rock', 'metal']]

如果我们将字符替换为数字,以便更清楚地知道它是3D数组,我们会得到类似的东西

If we replace the characters with numbers to make it a little clearer that it's a 3D array we get something like this

[[[1,2,3], [4,5,6]]]

当您使用 savemat 将其中任何一个保存到 .mat 文件时,它将被视为3D数组.

When you save either of these to a .mat file with savemat it's going to be treated as a 3D array.

如果您想要一个单元格数组,则必须手动创建一个numpy对象的numpy数组 .

If you instead want a cell array, you have to manually create a numpy array of numpy objects.

import scipy
import numpy as np

out = {'tags': np.array(['rock', 'metal'], dtype=np.object)}

scipy.io.savemat('test.mat', out)

然后在MATLAB或Octave中

Then within MATLAB or Octave

data = load('test.mat')
%    tags =
%    {
%      [1,1] = rock
%      [1,2] = metal
%    }

更新

对于嵌套单元格数组,要成为单元格数组的每个级别也必须是numpy对象的numpy数组

In the case of a nested cell array, each level that you would like to be a cell array must also be a numpy array of numpy objects

out = {'tags': np.array([
            np.array(['classical', 'pop'], dtype=np.object),    # A nested cell array
            'classical', 'classical', 'classical'], dtype=np.object)}

这篇关于使用Scipy将字典从Python保存到Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆