Reputation: 1000
I am having trouble converting a python list-of-list-of-list to a 3 dimensional numpy array.
a = [
[
[1,2,3,4], # = len 4
...
], # = len 58
...
] # = len 1245
when I call a = np.array(a)
on it, it reports shape as (1245,)
and I cannot reshape it. a.reshape(1245,58,4)
It gives me the error:
ValueError: cannot reshape array of size 1245 into shape (1245,58,4)
But If I print a[0] it gives me a 58 element list and a[0][0] gives me a 4 element list, as I expected, so the data is there.
I see plenty of stack exchange posts wanting to flatten it, but I just want to make it into a numpy array in the shape that it already is. I don't know why numpy.array()
is not seeing the other dimensions.
Upvotes: 1
Views: 3818
Reputation: 19124
Lists of the same level (the same numpy axis) need to be the same size. Otherwise you get an array of lists.
np.array([[0, 1], [2]])[0] # returns [0, 1]
np.array([[0, 1], [2, 3]])[0] # returns array([1, 2])
You can get around this by calling [pad
] on your lists before converting them to an array.
Furthermore the dimensions for reshape
"should be compatible with the original shape." Meaning (with the exception of a -1
value for inference) the product of the new dimensions should equal the product of the old dimensions.
For example in your case you have an array of shape (1245,)
so you could call:
a.reshape(83, 5, 3) # works
a.reshape(83, -1, 3) # works
a.reshape(83, 5, 5) # fails since 83 * 5 * 5 = 2075 != 1245
Upvotes: 2