繁体   English   中英

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

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

我有一个 numpy arrays 的列表,其形状是以下之一: (10,4,4,20), (10,4,6,20) 我想将列表转换为 numpy 数组。 因为,它们的形状不同,我不能把它们叠起来。 因此,我想创建 numpy 数组,将每个数组视为 object,如此处所示 我尝试了以下方法:

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

其中 a 是 numpy arrays 的列表。 两者都给我以下错误:

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

如何将该列表转换为 numpy 数组?

示例

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)

用例
我知道 numpy 对象数组效率不高,但我没有对它们进行任何操作。 通常,我有一个相同形状的列表 numpy arrays ,所以我可以轻松地将它们堆叠起来。 这个数组被传递给另一个 function,它只选择某些元素。 如果我的数据是 numpy 数组,我可以做b[[1,3,8]] 但我不能对列表做同样的事情。 如果我尝试与 list 相同,我会收到以下错误

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

如果列表 arrays 在第一个维度上不同,则np.array(alist)将创建一个 object dtype 数组。 但是在您的情况下,它们在第三个方面有所不同,从而产生了此错误。 实际上,它无法明确确定包含维度的结束位置以及对象的开始位置。

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)

相反,我们需要创建一个大小合适的 object 数组,并将列表复制到其中。 有时这个副本仍然会产生广播错误,但这里似乎没问题:

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