1

This is a code. I made a list including two ndarray with different shape.

d = []

a = np.arange(183).reshape(3,61)
b = np.arange(51).reshape(3,17)

d = [a,b]

np.array(d)

Error is like below.

 File "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/dev/workspace/rl/test/npcopy.py", line 10, in <module>
    np.array(d)
ValueError: could not broadcast input array from shape (3,61) into shape (3)

np.copy() works when two ndarrays' first shpae are different. but if not, it is not working like above.

if I change this code as below,

import numpy as np

d = []

a = np.arange(183).reshape(4, 61)
b = np.arange(51).reshape(3, 17)

d = [a,b]

np.array(d)

it works!! so weird!!

verystrongjoe
  • 3,831
  • 9
  • 35
  • 66

3 Answers3

3

As matrices are of different dimensions

> a = np.arange(183).reshape(3,61) b = np.arange(51).reshape(3,17)
> d=[np.array(a),np.array(b)] 
>  print(d) for output
> 
> or  d=[a,b]
>  np.concatenate(d, axis=1)
AnkushRasgon
  • 782
  • 1
  • 6
  • 14
1

When you try to make an array from arrays there are three possible results:

If the arrays have the same shape, the result is a higher dimension array:

In [295]: np.array((np.zeros((2,3),int),np.ones((2,3),int)))                 
Out[295]: 
array([[[0, 0, 0],
        [0, 0, 0]],

       [[1, 1, 1],
        [1, 1, 1]]])
In [296]: _.shape                                                            
Out[296]: (2, 2, 3)

If the arrays differ in shape, the result could be an object dtype array (similar to a list):

In [298]: np.array((np.zeros((2,3),int),np.ones((3,3),int)))                 
Out[298]: 
array([array([[0, 0, 0],
       [0, 0, 0]]),
       array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])], dtype=object)    # shape (2,)

But for some combinations of shapes, the result is an error:

In [301]: np.array((np.zeros((2,3),int),np.ones((2,4),int)))                 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-301-d67e6685581d> in <module>
----> 1 np.array((np.zeros((2,3),int),np.ones((2,4),int)))

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

In the error case, the first dimensions match, just as in your first case.

Sometimes to create an object array you have to start with a 'empty' one, and fill it. This is more reliable than the np.array(...) approach.

In [303]: arr = np.empty(2, object)                                          
In [304]: arr[:] = np.zeros((2,3),int),np.ones((2,4),int)                    
In [305]: arr                                                                
Out[305]: 
array([array([[0, 0, 0],
       [0, 0, 0]]),
       array([[1, 1, 1, 1],
       [1, 1, 1, 1]])], dtype=object)
In [306]: arr[:] = np.zeros((2,3),int),np.ones((2,3),int)                    
In [307]: arr                                                                
Out[307]: 
array([array([[0, 0, 0],
       [0, 0, 0]]),
       array([[1, 1, 1],
       [1, 1, 1]])], dtype=object)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • thanks for your answer! so this is a numpy bug? and you suggested workaround of it? – verystrongjoe May 22 '19 at 01:14
  • 1
    I wouldn't describe it as a bug. The normal, expected behavior is the first case. The others are, in a sense ways of making do with bad inputs. I'd like to see `np.array` throw an error in both cases unless an object dtype was specified. I suggested the most reliable way of making an object dtype array. – hpaulj May 22 '19 at 01:37
0

Instead of using np.copy(), I chose to use copy.deepcopy(). It works even if the first shape of two items in list are same.

verystrongjoe
  • 3,831
  • 9
  • 35
  • 66