简体   繁体   中英

Extracting inner array in python numpy

I have an array of shape (10,) and each element of that array contains another array of shape (9,9,1). How can I extract the inner array so that my final array has a shape (10,9,9,1).

Currently the array looks like:
array([array(...), array(...), ...])

Sure, all you need to do is use the numpy.reshape method.

# Just specify the array you want to reshape, and then a
new_arr = np.reshape(arr, [len(arr), len(arr[0]), len(arr[0][0]), len(arr[0][0][0])])

This is my lazy way of doing things because you specified the shape of the array and what you want it to be. If you didn't know the dimensions, and you wanted to expand it fully, I'd suggest creating a list variable and then using a while loop to check to see if the constituents of the array are arrays. If they are, you simply go deeper and if they're not, you add that dimension to the list . Then you could use that list as the second parameter.

Numpy does not allow you to have a non-rectangular array. If you want to convert a list of arrays into a single array, ALL arrays in the list must have the same shape. If they are all of the same shape, you can convert by:

arr = np.stack(your_list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM