ValueError:无法将输入数组从形状(224,224,3)广播到形状(224,224) [英] ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

查看:174
本文介绍了ValueError:无法将输入数组从形状(224,224,3)广播到形状(224,224)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list say, temp_list with following properties :

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

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

Now, when I am converting into numpy array,

x = np.array(temp_list)  

我遇到了错误:

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

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

Can someone help me here?

推荐答案

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

At least one item in your list is either not three dimensional; or it's (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 below.

也就是说,有问题的元素的shape != (?, 224, 3)
ndim != 3(?为非负整数).
那就是给你错误的地方.

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.

您需要解决此问题,才能将列表转换为四(或三)维数组.没有上下文,就无法说出要从3D项目中丢失尺寸还是要向2D项目添加尺寸(在第一种情况下),或者更改第二个或第三个尺寸(在第二种情况下).

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).

以下是错误的示例:

>>> 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)
>>> 

这篇关于ValueError:无法将输入数组从形状(224,224,3)广播到形状(224,224)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆