1

I have a numpy a = np.load('test.npy') file with these nested numpy array:

In [21]: a.shape
Out[21]: (6886, 3)

In [22]: a[0].shape
Out[22]: (3,)

In [23]: a[0][0].shape
Out[23]: (787, 6)

Is there a simple way to change a to be a 4 dimensional array with shape: (6886, 3, 787, 6)?

2
  • 2
    Looks like a.dtype is object. Are you sure all elements have the same shape (787,6)? If not the task is impossible. If they do, you can use np.stack (but have to first reshape the outer array). Commented Jan 23, 2019 at 17:44
  • You are correct. The problem is that not all arrays have the same (786,6) shape. How to add np.nan values for arrays that are short: e.g. (506,6) so that they all become same shape: (787,6)? Commented Jan 23, 2019 at 17:57

1 Answer 1

1

I would hate to do it this way, but all that comes to mind is making a second array of the desired shape and slice your data into it. I have to admit that I am having difficulty understanding the shapes of each sub-array...it seems counter intuitive. Anyway, this solution will be slow, but you can do it once and save the array and never do it again.

import numpy as np

a = np.load('test.npy')
b = np.full((6886, 3, 787, 6), np.nan)

for row in range(6886):
    for col in range(3):
        tmp = a[row][col]
        b[row, col, :tmp.shape[0], :tmp.shape[1]] = tmp

Does this make sense/work?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what I wanted to do, but I was hoping to find a way to avoid python for loops. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.