本文介绍了如何重塑()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()2D数组并组合奇数行和偶数行.

i want reshape() 2D-array and combine odd rows and even rows.

所需结果:

[[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]])

这篇关于如何重塑()numpy中奇数和偶数行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:26