什么是num2cell()的python/numpy等效项? [英] What is the python/numpy equivalent of num2cell()?
问题描述
我很不幸无法通过numpy数组将一些MATLAB代码转换为Python.
I'm unlucky enough to be converting some MATLAB code into Python via numpy arrays.
对num2cell()
是否有共识?
我个人认为这与Python/numpy语法背道而驰.这个想法是这样的:
Personally, I think this goes against Python/numpy syntax. The idea is this:
使用num2cell
,您将最终得到一个看起来像这样的数组
Using num2cell
, you'll end up in an array that looks like this
array([[0],[1],[2],[3],[4],[5],[6],[7],[8]])
请参见 MathWorks文档.
您可以通过列表理解以numpy的方式执行此操作:
You could do this in numpy with a list comprehension:
matlab_lunacy = np.array([[x] for x in range(0, 9)]
但是,为什么MATLAB用户使用此数据结构?
But why do MATLAB users use this data structure?
什么是numpy等效项?
What's the numpy equivalent?
推荐答案
在过去的好时光(约v.3.0),MATLAB只有一种数据结构,即矩阵.它可以包含数字或字符,并且始终为2d.
In the good old days (around v. 3.0) MATLAB had only one data structure, a matrix. It could contain numbers or characters, and was always 2d.
添加了单元格以包含更多常规对象,包括矩阵和字符串.他们仍然是2d.
Cells were added to contain more general objects, including matrices and strings. They were still 2d.
Python的列表为1d,但可以包含任何内容. numpy
基于Python构建,添加了多维数组.但是列表仍然可用.
Python had lists, which are 1d, but can contain anything. numpy
is built on Python, adding the multidimensional arrays. But lists are still available.
因此,将数组转换为列表的任何内容都可能等同于num2cell
-不完全精确,但是功能重叠.
So potentially anything that converts an array to a list is an equivalent to num2cell
- not exact, but with overlapping functionality.
In [246]: A=np.arange(24).reshape(2,3,4) # 3d array
包装在一个列表中,可以得到2个数组的列表(2d):
Wrapping in a list, gives us a list of 2 arrays (2d):
In [247]: B=list(A)
In [248]: B
Out[248]:
[array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]),
array([[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])]
tolist
方法完全转换为列表(嵌套).
tolist
method performs complete conversion to lists (nested).
In [249]: C=A.tolist()
In [250]: C
Out[250]:
[[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]],
[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]
list(A)
不常见,当使用tolist
时可能会错误使用.
list(A)
is not common, and may be used in error when tolist
is meant.
np.split(A,...)
与B
类似,但是子数组仍为3d.
np.split(A,...)
is similar to B
, but the subarrays are still 3d.
unpacking
甚至可以工作,主要是因为A
是可迭代的,[a for a in A]
在第一维上拆分A
.
unpacking
even works, basically because A
is an iterable, [a for a in A]
splits A
on the 1st dimension.
In [257]: a,b=A
In [258]: a
Out[258]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
有一个对象dtype,可用于将对象(包括其他数组)放入一个数组中.但是,正如许多SO问题所显示的那样,构建这些问题可能很棘手. np.array
尝试构造可能的最大维数数组.您必须执行一些技巧才能解决该问题.
There is an object dtype, with lets you put objects, including other arrays, in an array. But as has been shown in many SO questions, these can be tricky to construct. np.array
tries to construct the highest dimension array possible. You have to perform some tricks to get around that.
In [259]: Z=np.empty((2,),dtype=object)
In [260]: Z
Out[260]: array([None, None], dtype=object)
In [261]: Z[0]=A[0]
In [262]: Z[1]=A[1]
In [263]: Z
Out[263]:
array([ array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]),
array([[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])], dtype=object)
================
================
在八度会话中:
>> anum = [1,2,3,4]
anum =
1 2 3 4
>> acell = num2cell(anum)
acell =
{
[1,1] = 1
[1,2] = 2
[1,3] = 3
[1,4] = 4
}
>> save -7 test.mat anum acell
scipy.io.loatmat
版本
In [1822]: data = io.loadmat('../test.mat')
In [1823]: data
Out[1823]:
{'__globals__': [],
'__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.0.0,
2016-10-27 00:59:27 UTC',
'__version__': '1.0',
'acell': array([[array([[ 1.]]), array([[ 2.]]), array([[ 3.]]),
array([[ 4.]])]], dtype=object),
'anum': array([[ 1., 2., 3., 4.]])}
matrix
呈现为2d数组; cell
作为对象类型数组(2d),在这种情况下,包含2d数组.
The matrix
is rendered as a 2d array; the cell
as an object type array (2d), containing, in this case, 2d arrays.
这篇关于什么是num2cell()的python/numpy等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!