Numpy array of numpy arrays has 1D shape

arrays, numpy, python

Solution

`A.dtype` is `O`, object, `B.dtype` is `float`.

`A` is a 1d array that contains objects, which happen to be arrays. They could just as well be lists or None`.

`B` is a 2d array of floats. Indexing one row of `B` gives a 1d array.

So `A[0]` and `B[0]` can appear to produce the same thing, but the selection process is different.

Try `np.concatenate(A)`, or `np.vstack(A)`. Both of these then treat `A` as a list of arrays, and join them either in 1 or 2d.

Converting object arrays to regular comes up quite often.

Converting a 3D List to a 3D NumPy array is a little more general that what you need, but gives a lot of useful information.

also

Convert a numpy array of lists to a numpy array

==================

In [28]: A=np.empty((5,),object)
In [31]: A
Out[31]: array([None, None, None, None, None], dtype=object)
In [32]: for i in range(5):A[i]=np.zeros((3,),int)
In [33]: A
Out[33]: 
array([array([0, 0, 0]), array([0, 0, 0]), array([0, 0, 0]),
       array([0, 0, 0]), array([0, 0, 0])], dtype=object)
In [34]: print(A)
[array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0])
 array([0, 0, 0])]
In [35]: np.vstack(A)
Out[35]: 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])

Edit

np.stack(A)

can join the arrays on a new leading axis.

If the subarrays differ in shape, these 'stack' functions will raise an error. It's up to you to find the problem array(s).

Problem

I have two numpy arrays of arrays (A and B). They look something like this when printed: A: ``` [array([0, 0, 0]) array([0, 0, 0]) array([1, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 1]) array([0, 0, 0]) array([1, 0, 0]) array([0, 0, 1]) array([0, 0, 0]) array([0, 0, 0]) array([0, 0, 0]) array([1, 0, 0]) array([0, 0, 1]) array([0, 0, 0])] ``` B: ``` [[ 4.302135e-01 4.320091e-01 4.302135e-01 4.302135e-01 1.172584e+08] [ 4.097128e-01 4.097128e-01 4.077675e-01 4.077675e-01 4.397120e+07] [ 3.796353e-01 3.796353e-01 3.778396e-01 3.778396e-01 2.643200e+07] [ 3.871173e-01 3.890626e-01 3.871173e-01 3.871173e-01 2.161040e+07] [ 3.984899e-01 4.002856e-01 3.984899e-01 3.984899e-01 1.836240e+07] [ 4.227315e-01 4.246768e-01 4.227315e-01 4.227315e-01 1.215760e+07] [ 4.433817e-01 4.451774e-01 4.433817e-01 4.433817e-01 9.340800e+06] [ 4.620867e-01 4.638823e-01 4.620867e-01 4.620867e-01 1.173760e+07]] ``` `type(A)`, `type(A[0])`, `type(B)`, `type(B[0])` are all `<class 'numpy.ndarray'>`. However, `A.shape` is `(20,)`, while `B.shape` is `(8, 5)`. Question 1: Why is `A.shape` one-dimensional, and how do I make it two-dimensional like `B.shape`? They're both arrays of arrays, right? Question 2, possibly related to Q1: Why does printing `A` show the calls of `array()`, while printing `B` doesn't, and why do the elements of the subarrays of `B` not have commas in-between them? Thanks in advance.

Original source

Related problems