如何有效地从 loadmat 函数生成的嵌套 numpy 数组中提取值? [英] How to efficiently extract values from nested numpy arrays generated by loadmat function?
问题描述
python 中是否有更有效的方法从嵌套的 python 列表中提取数据,例如 A = array([[array([[12000000]])]], dtype=object)
.我一直在使用A[0][0][0][0]
,当你有很多像A这样的数据时,它似乎不是一个有效的方法.
Is there a more efficient method in python to extract data from a nested python list such as A = array([[array([[12000000]])]], dtype=object)
. I have been using A[0][0][0][0]
, it does not seem to be an efficinet method when you have lots of data like A.
我也用过numpy.squeeeze(array([[array([[12000000]])]], dtype=object))
但这给了我
I have also used
numpy.squeeeze(array([[array([[12000000]])]], dtype=object))
but this gives me
array(array([[12000000]]), dtype=object)
PS:嵌套数组是由 scipy 模块中的 loadmat()
函数生成的,用于加载由嵌套结构组成的 .mat 文件.
PS: The nested array was generated by loadmat()
function in scipy module to load a .mat file which consists of nested structures.
推荐答案
创建这样的数组有点乏味,但是 loadmat
可以处理 MATLAB 单元格和二维矩阵:
Creating such an array is a bit tedious, but loadmat
does it to handle the MATLAB cells and 2d matrix:
In [5]: A = np.empty((1,1),object)
In [6]: A[0,0] = np.array([[1.23]])
In [7]: A
Out[7]: array([[array([[ 1.23]])]], dtype=object)
In [8]: A.any()
Out[8]: array([[ 1.23]])
In [9]: A.shape
Out[9]: (1, 1)
squeeze
压缩形状,但不跨越 object
边界
squeeze
compresses the shape, but does not cross the object
boundary
In [10]: np.squeeze(A)
Out[10]: array(array([[ 1.23]]), dtype=object)
但是如果您在数组中有一项(无论形状如何)item()
可以提取它.索引也有效,A[0,0]
but if you have one item in an array (regardless of shape) item()
can extract it. Indexing also works, A[0,0]
In [11]: np.squeeze(A).item()
Out[11]: array([[ 1.23]])
item
再次从该内部数组中提取数字:
item
again to extract the number from that inner array:
In [12]: np.squeeze(A).item().item()
Out[12]: 1.23
或者我们甚至不需要squeeze
:
In [13]: A.item().item()
Out[13]: 1.23
loadmat
有一个 squeeze_me
参数.
索引同样简单:
In [17]: A[0,0]
Out[17]: array([[ 1.23]])
In [18]: A[0,0][0,0]
Out[18]: 1.23
astype
也可以工作(尽管它可能对维数很挑剔).
astype
can also work (though it can be picky about the number of dimensions).
In [21]: A.astype(float)
Out[21]: array([[ 1.23]])
对于像效率这样的单项数组,问题不大.所有这些方法都很快.当数组有很多项,或者项本身很大时,事情会变得更加复杂.
With single item arrays like efficiency isn't much of an issue. All these methods are quick. Things become more complicated when the array has many items, or the items are themselves large.
这篇关于如何有效地从 loadmat 函数生成的嵌套 numpy 数组中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!