简体   繁体   English

4-D Numpy数组的互换顺序

[英]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. 我有一个尺寸为96x96x3x1000的4-D Numpy数组-这些对应于我已导入的图像数据集:96X96像素各1000幅图像,每个像素RGB值。

However, I need to iterate over flattened arrays for each image, ie. 但是,我需要遍历每个图像的展平数组,即。 only a 2-D array [1000][96*96*3]. 仅是二维数组[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 ? 我想问一下是否有一种更简单的切片方法来交换ndarray的顺序?

Thanks 谢谢

You can change the ordering of the axes using numpy.swapaxes 您可以使用numpy.swapaxes更改轴的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 您也可以更改轴的顺序在与开始numpy.transpose ,然后应用reshape

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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