简体   繁体   中英

Interchange ordering of 4-D Numpy array

I have a 4-D Numpy array of dimensions 96x96x3x1000 - these correspond to an image dataset that I have imported : 1000 images each of 96X96 pixels and RGB values for each pixel.

However, I need to iterate over flattened arrays for each image, ie. only a 2-D array [1000][96*96*3]. I managed to transform the given array by first doing

    a.reshape(-1,a.size[3])

and then assigning each column to an image using a loop. I wanted to ask if there is a simpler/slicing method for interchanging the ordering of ndarrays ?

Thanks

You can change the ordering of the axes using numpy.swapaxes

a.reshape(-1,1000).swapaxes(0,1)

or simply tranposing it

a.reshape(-1,1000).T

You can also change the ordering of the axis at the beginning with numpy.transpose and then apply reshape

a.transpose([3,0,1,2]).reshape(1000,-1)

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