Python ValueError:无法将输入数组从形状 (224,224,3) 广播到形状 (224,224)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43977463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:35:48  来源:igfitidea点击:

ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

pythonnumpy

提问by neel

I have a list say, temp_list with following properties :

我有一个列表说,temp_list 具有以下属性:

len(temp_list) = 9260  
temp_list[0].shape = (224,224,3)  

Now, when I am converting into numpy array,

现在,当我转换成 numpy 数组时,

x = np.array(temp_list)  

I am getting the error :

我收到错误:

ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)  

Can someone help me here?

有人可以在这里帮助我吗?

回答by

At least one item in your list is either not three dimensional, or its second or third dimension does not match the other elements. If only the first dimension does not match, the arrays are still matched, but as individual objects, no attempt is made to reconcile them into a new (four dimensional) array. Some examples are below:

列表中至少有一项不是三维的,或者它的第二维或第三维与其他元素不匹配。如果只有第一维不匹配,数组仍然匹配,但作为单独的对象,不会尝试将它们协调为新的(四维)数组。一些例子如下:

That is, the offending element's shape != (?, 224, 3),
or ndim != 3(with the ?being non-negative integer).
That is what is giving you the error.

也就是说,违规元素的shape != (?, 224, 3),
or ndim != 3?非负整数)。
这就是给你错误的原因。

You'll need to fix that, to be able to turn your list into a four (or three) dimensional array. Without context, it is impossible to say if you want to lose a dimension from the 3D items or add one to the 2D items (in the first case), or change the second or third dimension (in the second case).

您需要解决这个问题,以便能够将您的列表变成一个四(或三)维数组。没有上下文,就不可能说是要从 3D 项目中丢失一个维度还是向 2D 项目添加一个维度(在第一种情况下),或者更改第二个或第三个维度(在第二种情况下)。



Here's an example of the error:

这是错误的示例:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))]
>>> np.array(a)
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

or, different type of input, but the same error:

或者,不同类型的输入,但同样的错误:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224,13))]
>>> np.array(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

Alternatively, similar but with a different error message:

或者,类似但有不同的错误消息:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,100,3))]
>>> np.array(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (224,224,3) into shape (224)

But the following will work, albeit with different results than (presumably) intended:

但是以下方法会起作用,尽管结果与(大概)预期的结果不同:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((10,224,3))]
>>> np.array(a)
# long output omitted
>>> newa = np.array(a)
>>> newa.shape
3  # oops
>>> newa.dtype
dtype('O')
>>> newa[0].shape
(224, 224, 3)
>>> newa[1].shape
(224, 224, 3)
>>> newa[2].shape
(10, 224, 3)
>>> 

回答by Jagesh Maharjan

Yea, Indeed @Evert answer is perfectly correct. In addition I'll like to add one more reason that could encounter such error.

是的,@Evert 的答案确实是完全正确的。此外,我想再添加一个可能遇到此类错误的原因。

>>> np.array([np.zeros((20,200)),np.zeros((20,200)),np.zeros((20,200))])

This will be perfectly fine, However, This leads to error:

这会很好,但是,这会导致错误:

>>> np.array([np.zeros((20,200)),np.zeros((20,200)),np.zeros((20,201))])

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

The numpy arry within the list, must also be the same size.

列表中的 numpy arry 也必须具有相同的大小。

回答by Yinjie Gao

You can covert numpy.ndarrayto objectusing astype(object)

你可以隐蔽numpy.ndarrayobject使用astype(object)

This will work:

这将起作用:

>>> a = [np.zeros((224,224,3)).astype(object), np.zeros((224,224,3)).astype(object), np.zeros((224,224,13)).astype(object)]