[英]Create ndarray from list with mismatched axis>0 size
I want to save a list of Numpy arrays to a file. 我想将Numpy数组的列表保存到文件中。 The list is of the following shape:
该列表具有以下形状:
my_list = [np.ones((2, 515, 3)), np.ones((2, 853, 3))]
However, when I try to save it using np.savez
, the list tries to get converted into an Numpy array. 但是,当我尝试使用
np.savez
保存它时,该列表尝试将其转换为Numpy数组。 Doing np.array(my_list, dtype='object')
gives the error: 这样做
np.array(my_list, dtype='object')
给出错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-6fcbd172df30> in <module>()
----> 1 np.array([np.ones((2, 515, 3)), np.ones((2, 853, 3))], dtype='object')
ValueError: could not broadcast input array from shape (2,515,3) into shape (2)
However, if the axis=0
dimension is mismatched instead of the axis=1
dimension, such as my_list = [np.ones((515, 3)), np.ones((853, 3))]
, I no longer get this error. 但是,如果
axis=0
尺寸不匹配,而不是axis=1
尺寸不匹配,例如my_list = [np.ones((515, 3)), np.ones((853, 3))]
,我将不再得到这个错误。
Why does the mistmatched axis dimension affect the ability to Numpy array from objects? 为什么雾匹配的轴尺寸会影响从对象进行Numpy数组的能力?
Although there are work-arounds possible to break up the array into a save-able format, I'm mostly interested about why the conversion failure is happening and how to get around it. 尽管有一些变通方法可以将数组分解为可保存的格式,但是我最感兴趣的是为什么发生转换失败以及如何解决它。
In [77]: my_list = [np.ones((2, 515, 3)), np.ones((2, 853, 3))]
Save with the *args
parameter, or with a **kwargs
dictionary 使用
*args
参数或**kwargs
字典保存
In [78]: np.savez('test',*my_list)
In [79]: ll = np.load('test.npz')
In [80]: list(ll.keys())
Out[80]: ['arr_0', 'arr_1']
In [81]: ll['arr_0'].shape
Out[81]: (2, 515, 3)
In [82]: ll['arr_1'].shape
Out[82]: (2, 853, 3)
or with named keywarods/dictionary 或带有命名的键盘提示/字典
In [85]: np.savez('test',x=my_list[0],y=my_list[1])
np.savez('test', my_list)
, first turns my_list
into an array - or tries to np.savez('test', my_list)
,首先将my_list
转换为数组-或尝试
In [83]: np.array(my_list)
...
ValueError: could not broadcast input array from shape (2,515,3) into shape (2)
When trying to create an array from a list of arrays there are 3 possible outcomes: a higher dimensional array (if dimensions match), an object array (if dimensions don't match), or this error (if the dimensions sort-of match). 尝试从数组列表创建数组时,可能会有3种结果:高维数组(如果尺寸匹配),对象数组(如果尺寸不匹配)或此错误(如果尺寸排序匹配) )。
The object dtype case: 对象dtype的情况:
In [86]: arr=np.array([np.ones((515, 3)), np.ones((853, 3))])
In [87]: arr.shape
Out[87]: (2,)
In [88]: arr.dtype
Out[88]: dtype('O')
The surest way to create an object array is to preallocate it 创建对象数组的最可靠方法是对其进行预分配
In [90]: arr = np.zeros((2,), object)
In [91]: arr[...]=my_list
The shape of arr
has to match the nesting of the sublists/arrays in my_list
, otherwise you'll get broadcasting errors. arr
的形状必须匹配my_list
列表/数组的嵌套,否则会出现广播错误。 arr
can be reshaped after loading. 加载后可以重塑
arr
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.