我有一个列表,说temp_list具有以下属性:
len(temp_list) = 9260 temp_list[0].shape = (224,224,3)
现在,当我转换为numpy数组时,
x = np.array(temp_list)
我收到错误消息:
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)
有人可以帮我吗?
列表中至少有一项不是三维的,或者其第二维或第三维与其他元素不匹配。如果仅第一维不匹配,则数组仍然匹配,但是作为单独的对象,不会尝试将它们协调为新的(四维)数组。下面是一些示例:
也就是说,冒犯元件的shape != (?, 224, 3), 或ndim != 3(与?是非负整数)。 那就是给你错误的原因。
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: could not broadcast input array from shape (224,224,3) into shape (224,224)
或,不同类型的输入,但相同的错误:
>>> 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)
或者,类似但带有不同的错误消息:
>>> 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)
但是,以下结果将起作用,尽管结果与预期的结果不同:
>>> 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) >>>