1

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.

2
  • Lists of the same level (the same numpy axis) need to be the same size. Otherwise you get an array of lists.
    – Alex
    Commented Jan 25, 2018 at 17:10
  • Good catch. I though they were, but your comment made me reevaluate that assumption. I while creating the lists, I was looping over the wrong variable - one that changed loop to loop.
    – user9170
    Commented Jan 25, 2018 at 17:20

1 Answer 1

2

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.