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

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

问题描述

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

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

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

x = np.array(temp_list)

我收到错误:

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

有人可以帮我吗?

解决方案

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

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

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

<小时>

以下是错误示例:

<预><代码>>>>a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))]>>>np.array(a)ValueError:无法将输入数组从形状 (224,224,3) 广播到形状 (224,224)

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

<预><代码>>>>a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224,13))]>>>np.array(a)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中ValueError:无法将输入数组从形状 (224,224,3) 广播到形状 (224,224)

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

<预><代码>>>>a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,100,3))]>>>np.array(a)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中ValueError:无法将输入数组从形状 (224,224,3) 广播到形状 (224)

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

<预><代码>>>>a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((10,224,3))]>>>np.array(a)# 省略长输出>>>newa = np.array(a)>>>newa.shape3 # 哎呀>>>新类型dtype('O')>>>newa[0].shape(224, 224, 3)>>>newa[1].shape(224, 224, 3)>>>newa[2].shape(10, 224, 3)>>>

I have a list say, temp_list with following properties :

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

Now, when I am converting into numpy array,

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?

解决方案

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.

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


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

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

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