[英]How to reshape () the sum of odd and even rows in numpy
示例1:
a = np.array([[[1,11,111],[2,22,222]],
[[3,33,333],[4,44,444]],
[[5,55,555],[6,66,666]],[[7,77,777],[8,88,888]]])
>>> a
array([[[ 1, 11, 111],
[ 2, 22, 222]],
[[ 3, 33, 333],
[ 4, 44, 444]],
[[ 5, 55, 555],
[ 6, 66, 666]],
[[ 7, 77, 777],
[ 8, 88, 888]]])
我想要 reshape() 二维数组并组合奇数行和偶数行。
想要的结果:
[[1, 11, 111, 3, 33, 333, 5, 55, 555, 7, 77, 777],
[2, 22, 222, 4, 44, 444, 6, 66, 666, 8, 88, 888]]
我怎样才能像上面那样输出?
Permute axes and reshape to 2D
-
In [14]: a
Out[14]:
array([[[ 1, 11, 111],
[ 2, 22, 222]],
[[ 3, 33, 333],
[ 4, 44, 444]],
[[ 5, 55, 555],
[ 6, 66, 666]],
[[ 7, 77, 777],
[ 8, 88, 888]]])
In [15]: a.swapaxes(0,1).reshape(a.shape[1],-1)
Out[15]:
array([[ 1, 11, 111, 3, 33, 333, 5, 55, 555, 7, 77, 777],
[ 2, 22, 222, 4, 44, 444, 6, 66, 666, 8, 88, 888]])
>>> np.array(list(zip(*a))).reshape(2,12)
array([[ 1, 11, 111, 3, 33, 333, 5, 55, 555, 7, 77, 777],
[ 2, 22, 222, 4, 44, 444, 6, 66, 666, 8, 88, 888]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.