Python 3d Numpy 数组到 2d
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13990465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
3d Numpy array to 2d
提问by poeticcapybara
I have a 3d matrix like this
我有一个像这样的 3d 矩阵
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]])
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)?
有没有一种方法可以不显式地对它们进行 hstacking(和/或 vstacking)或添加额外的维度和重塑(不确定这是否可行)?
Thanks,
谢谢,
采纳答案by unutbu
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]])
I've posted more general functions for reshaping/unshaping arrays into blocks, here.
我已经发布了更多用于将数组整形/取消整形为块的通用函数,here。

