0

I am having a database of 7000 objects (list_of_objects), each one of these files contains a numpy array with size of 10x5x50x50x3. I would like to create a 5d numpy array that will contain 7000*10x5x50x50x3. I tried to do so using two for-loops. My sample code:

fnl_lst = []
for object in list_of_objects:
     my_array = read_array(object) # size 10x5x50x50x3
     for ind in my_array:
        fnl_lst.append(ind)
fnl_lst= np.asarray( fnl_lst) # print(fnl_lst) -> (70000,)

That code result in the end in a nested numpy array which contains 70000 arrays each of them has a size of 5x50x50x3. However, I would like instead to build a 5d array with size 70000x5x50x50x3. How can I do that instead?

5
  • 1
    Possible duplicate of How to convert list of numpy arrays into single numpy array? Commented Apr 9, 2019 at 16:17
  • 1
    Are you sure all the ind are the same shape? What does np.vstack([read_array(o) for o in list_of_objects]) produce? Commented Apr 9, 2019 at 16:33
  • @hpaulj I will cross-check. Maybe that is my issue that the ind for some reason do not have the same size. Commented Apr 9, 2019 at 17:17
  • What is fnl_lst.dtype? If object, then yes, there's some variation in the shapes. Commented Apr 9, 2019 at 17:45
  • True one files have different size and could not monitor it. Now it works properly. Commented Apr 9, 2019 at 18:47

1 Answer 1

2
fnl_lst = np.stack([ind for ind in read_array(obj) for obj in list_of_objects])

or, just append to the existing code:

fnl_lst = np.stack(fnl_lst)

UPD: by hpaulj's comment, if my_array is indeed 10x5x50x50x3, this might be enough:

fnl_lst = np.stack([read_array(obj) for obj in list_of_objects])
Sign up to request clarification or add additional context in comments.

Comments

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.