简体   繁体   English

numpy 数组包含多维 numpy arrays 具有可变形状

[英]numpy array containing multi-dimension numpy arrays with variable shape

I have a list of numpy arrays, whose shape is one of the following: (10,4,4,20), (10,4,6,20) .我有一个 numpy arrays 的列表,其形状是以下之一: (10,4,4,20), (10,4,6,20) I want to convert the list to a numpy array.我想将列表转换为 numpy 数组。 Since, they are of different shapes, I can't just stack them.因为,它们的形状不同,我不能把它们叠起来。 So, I thought of creating numpy array considering each array as an object, as in here .因此,我想创建 numpy 数组,将每个数组视为 object,如此处所示 I tried the below:我尝试了以下方法:

b = numpy.array(a)
b = numpy.array(a, dtype=object)

where a is the list of numpy arrays.其中 a 是 numpy arrays 的列表。 Both are giving me the following error:两者都给我以下错误:

ValueError: could not broadcast input array from shape (10,4,4,20) into shape (10,4)

How can I convert that list to numpy array?如何将该列表转换为 numpy 数组?

Example :示例

import numpy
a = [numpy.random.random((10,4,4,20)),
     numpy.random.random((10,4,6,20)),
     numpy.random.random((10,4,6,20)),
     numpy.random.random((10,4,4,20)),
     numpy.random.random((10,4,6,20)),
     numpy.random.random((10,4,6,20)),
     numpy.random.random((10,4,4,20)),
     numpy.random.random((10,4,4,20)),
     numpy.random.random((10,4,6,20))
    ]
b = numpy.array(a)

Use Case :用例
I know numpy array of objects are not efficient, but I'm not doing any operations on them.我知道 numpy 对象数组效率不高,但我没有对它们进行任何操作。 Usually, I have a list of same shape numpy arrays and so I can easily stack them.通常,我有一个相同形状的列表 numpy arrays ,所以我可以轻松地将它们堆叠起来。 This array is passed to another function, which selects certain elements only.这个数组被传递给另一个 function,它只选择某些元素。 If my data is numpy array, I can just do b[[1,3,8]] .如果我的数据是 numpy 数组,我可以做b[[1,3,8]] But I can't do the same with list.但我不能对列表做同样的事情。 I get the following error if I try the same with list如果我尝试与 list 相同,我会收到以下错误

c = a[[1,3,8]]
TypeError: list indices must be integers or slices, not list

np.array(alist) will make an object dtype array if the list arrays differ in the first dimension.如果列表 arrays 在第一个维度上不同,则np.array(alist)将创建一个 object dtype 数组。 But in your case they differ in the 3rd, producing this error.但是在您的情况下,它们在第三个方面有所不同,从而产生了此错误。 In effect, it can't unambiguously determine where the containing dimension ends, and where the objects begin.实际上,它无法明确确定包含维度的结束位置以及对象的开始位置。

In [270]: alist = [np.ones((10,4,4,20),int), np.zeros((10,4,6,20),int)]                                
In [271]: arr = np.array(alist)                                                                        
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-271-3fd8e9bd05a9> in <module>
----> 1 arr = np.array(alist)

ValueError: could not broadcast input array from shape (10,4,4,20) into shape (10,4)

Instead we need to make an object array of the right size, and copy the list to it.相反,我们需要创建一个大小合适的 object 数组,并将列表复制到其中。 Sometimes this copy still produces broadcasting errors, but here it seems to be ok:有时这个副本仍然会产生广播错误,但这里似乎没问题:

In [272]: arr = np.empty(2, object)                                                                    
In [273]: arr                                                                                          
Out[273]: array([None, None], dtype=object)
In [274]: arr[:] = alist                                                                               
In [275]: arr                                                                                          
Out[275]: 
array([array([[[[1, 1, 1, ..., 1, 1, 1],
         [1, 1, 1, ..., 1, 1, 1],
         [1, 1, 1, ..., 1, 1, 1],
...
         [0, 0, 0, ..., 0, 0, 0],
         [0, 0, 0, ..., 0, 0, 0]]]])], dtype=object)
In [276]: arr[0].shape                                                                                 
Out[276]: (10, 4, 4, 20)
In [277]: arr[1].shape                                                                                 
Out[277]: (10, 4, 6, 20)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM