重新排列4D Numpy数组 [英] Rearranging a 4d numpy array

查看:98
本文介绍了重新排列4D Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4d numpy数组,它表示具有3d实例的数据集. 可以说数组的形状为(32, 32, 3, 73257).

如何将数组的形状更改为(73257, 32, 32, 3)?

-问题更新 似乎rollaxis和transpose都可以解决问题.

感谢您的回复!

解决方案

np.transpose函数完全可以实现您想要的功能,您可以传递轴参数来控制要交换的轴:

a = np.empty((32, 32, 3, 73257))
b = np.transpose(a, (3, 0, 1, 2))

b的轴是a的轴的置换版本:b的轴0是a的第3轴,b的轴1是a的第0轴,依此类推...

这样,您可以在第二或第三位指定要使用32号尺寸的轴:

b = np.transpose(a, (3, 1, 0, 2))

还给出了所需形状的数组,但与前一个形状不同.

I have a 4d numpy array which represents a dataset with 3d instances. Lets say that the shape of the array is (32, 32, 3, 73257).

How can i change the shape of the array to (73257, 32, 32, 3)?

--- Question update It seems that both rollaxis and transpose do the trick.

Thanx for replying!

解决方案

The np.transpose function does exactly what you want, you can pass an axis argument which controls which axis you want to swap:

a = np.empty((32, 32, 3, 73257))
b = np.transpose(a, (3, 0, 1, 2))

The axis of b are permuted versions of the ones of a: the axis 0 of b is the 3-rd axis of a, the axis 1 of b is the 0-th axis of a, etc...

That way, you can specify which of the axis of size 32 you want in second or in third place:

b = np.transpose(a, (3, 1, 0, 2))

Also gives an array of the desired shape, but is different from the previous one.

这篇关于重新排列4D Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆