In [268]: x = np.ones((4,3),int); y = np.zeros((4,1),int)
With this combination of shapes (same 1st dimension) np.array raises an error:
In [269]: np.array((x,y))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-269-8419f5dd7aa8> in <module>
----> 1 np.array((x,y))
ValueError: could not broadcast input array from shape (4,3) into shape (4)
WIth different 1st dimensions, it makes an object dtype array - basically a glorified (or debased) list of arrays:
In [270]: np.array((x.T,y.T))
Out[270]:
array([array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]),
array([[0, 0, 0, 0]])], dtype=object)
np.array tries to make a regular multidimensional array. If the shapes don't allow that, it has to fall back on the object dtype, or fail.
We can pre allocate the object array, and assign the elements:
In [271]: res = np.empty((2,),object)
In [272]: res
Out[272]: array([None, None], dtype=object)
In [273]: res[0]=x; res[1] = y
In [274]: res
Out[274]:
array([array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]),
array([[0],
[0],
[0],
[0]])], dtype=object)
A potentially useful structured array can be constructed with:
In [278]: res1 = np.zeros((4,), dtype=[('x',int,(3,)),('y',int,(1,))])
In [279]: res1
Out[279]:
array([([0, 0, 0], [0]), ([0, 0, 0], [0]), ([0, 0, 0], [0]),
([0, 0, 0], [0])], dtype=[('x', '<i8', (3,)), ('y', '<i8', (1,))])
In [280]: res1['x']=x
In [281]: res1['y']=y
In [282]: res1
Out[282]:
array([([1, 1, 1], [0]), ([1, 1, 1], [0]), ([1, 1, 1], [0]),
([1, 1, 1], [0])], dtype=[('x', '<i8', (3,)), ('y', '<i8', (1,))])
In [283]: res1[0]
Out[283]: ([1, 1, 1], [0])
If the size 4 dimension is put in the dtype, the result is a 0d array:
In [284]: np.array((x,y), dtype=[('x',int,(4,3)),('y',int,(4,1))])
Out[284]:
array(([[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[0], [0], [0], [0]]),
dtype=[('x', '<i8', (4, 3)), ('y', '<i8', (4, 1))])
In [285]: res2 = np.array((x,y), dtype=[('x',int,(4,3)),('y',int,(4,1))])
In [287]: res2.shape
Out[287]: ()
Practically this is more like a dictionary, {'x':x, 'y':y} than an array.
np.arrayraises an error.. There are ways around it, but ...