In [5]: a1 = np.array([[1,2], [3,4], [5,6]])
...: a2 = np.array([7,3])
...: a3 = np.array([1])
The best way is to make a 'blank' array of the desired dtype and shape:
In [6]: a_parent = np.empty(3, object)
In [7]: a_parent
Out[7]: array([None, None, None], dtype=object)
and 'fill' it from a list of the desired arrays (or other objects):
In [13]: a_parent[:] = [a1,a2,a3]
In [14]: a_parent
Out[14]:
array([array([[1, 2],
[3, 4],
[5, 6]]), array([7, 3]),
array([1])], dtype=object)
I'm sure loadmat
uses this method.
Passing the list directly to np.array
may work, but v1.19 wants us to include the object
dtype:
In [10]: np.array([a1,a2,a3])
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
#!/usr/bin/python3
Out[10]:
array([array([[1, 2],
[3, 4],
[5, 6]]), array([7, 3]),
array([1])], dtype=object)
This does not work if the arrays are all the same shape:
In [11]: np.array([a1,a1])
Out[11]:
array([[[1, 2],
[3, 4],
[5, 6]],
[[1, 2],
[3, 4],
[5, 6]]])
And for some shape combinations we get an error.
In [15]: a_parent[:] = [a3,a3,a3]
In [16]: a_parent
Out[16]: array([array([1]), array([1]), array([1])], dtype=object)