ValueError: cannot pass input array from form (224,224,3) to form (224,224)

I have a say, temp_list with the following properties:

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

Now when I convert to a numpy array,

x = np.array(temp_list)  

I get an error message:

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

Can anyone help me out here?

+26
source share
3 answers

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

shape != (?, 224, 3) shape != (?, 224, 3),
ndim != 3 ( ? ).
, .

, ( ) . , ( ), ( ).


:

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

, @Evert . , , .

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

, :

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

numpy .

+7

numpy.ndarray object astype(object)

:

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

Source: https://habr.com/ru/post/1017266/


All Articles