3d Numpy阵列到2d [英] 3d Numpy array to 2d
本文介绍了3d Numpy阵列到2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的3d矩阵
I have a 3d matrix like this
arange(16).reshape((4,2,2))
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15]]])
,并希望将它们以网格格式堆叠,最后以
and would like to stack them in grid format, ending up with
array([[ 0, 1, 4, 5],
[ 2, 3, 6, 7],
[ 8, 9, 12, 13],
[10, 11, 14, 15]])
有没有一种方法,而无需显式地将它们进行hstack(和/或vstacking)或添加额外的尺寸并重塑(不确定这样做是否可行)?
Is there a way of doing without explicitly hstacking (and/or vstacking) them or adding an extra dimension and reshaping (not sure this would work)?
谢谢
推荐答案
In [27]: x = np.arange(16).reshape((4,2,2))
In [28]: x.reshape(2,2,2,2).swapaxes(1,2).reshape(4,-1)
Out[28]:
array([[ 0, 1, 4, 5],
[ 2, 3, 6, 7],
[ 8, 9, 12, 13],
[10, 11, 14, 15]])
这篇关于3d Numpy阵列到2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文