首页 > 解决方案 > 包含具有可变形状的多维 numpy 数组的 numpy 数组

问题描述

我有一个 numpy 数组列表,其形状为以下之一(10,4,4,20), (10,4,6,20):我想将列表转换为 numpy 数组。因为,它们的形状不同,我不能把它们叠起来。所以,我想创建 numpy 数组,将每个数组视为一个对象,就像在这里一样。我尝试了以下方法:

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

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

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 数组的列表,因此我可以轻松地堆叠它们。这个数组被传递给另一个函数,它只选择某些元素。如果我的数据是 numpy 数组,我可以做b[[1,3,8]]. 但我不能对列表做同样的事情。如果我尝试与 list 相同,我会收到以下错误

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

标签: pythonarraysnumpytypeerrorvalueerror

解决方案


np.array(alist)如果列表数组的第一维不同,则将创建一个对象 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)

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

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)

推荐阅读