简体   繁体   English

整形重塑/尺寸改变

[英]Numpy reshape/dimension change

I want to change an array such as 我想更改一个数组,例如

array([[  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.]])

To

array([[[7.,   3.],
        [7.,   3.],
        [7.,   3.],
        [7.,   3.]],

       [[14.,   1.],
        [14.,   1.],
        [14.,   1.],
        [14.,   1.]],

       [[9.,   17.],
        [9.,    17.],
        [9.,   17.],
        [9.,   17.]]])

I thought I would manage with reshape, but none of the order statements work in this case. 我以为我可以重塑形状,但是在这种情况下,任何订单声明都无法工作。 Currently, I am doing it this way 目前,我正在这样做

np.vstack([mat[:, i-2:i] for i in range(2, mat.shape[1]+1, 2)]).reshape(3,-1,2)

And I was wondering if there is a better way to do it 我想知道是否有更好的方法

reshape and then swapaxes : reshape然后swapaxes

import numpy as np

a = np.array(
      [[  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.],
       [  7.,   3.,  14.,   1.,   9.,  17.]])

a.reshape((a.shape[0], -1, 2)).swapaxes(0, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM