numpy:沿着指定的轴调整数组的形状 [英] Numpy: Reshape array along a specified axis

查看:93
本文介绍了numpy:沿着指定的轴调整数组的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

x = np.arange(24).reshape((2,3,2,2))
array([[[[ 0,  1],
     [ 2,  3]],

    [[ 4,  5],
     [ 6,  7]],

    [[ 8,  9],
     [10, 11]]],


   [[[12, 13],
     [14, 15]],

    [[16, 17],
     [18, 19]],

    [[20, 21],
     [22, 23]]]])

我想将其重塑为(3,4,2)数组,如下所示:

I would like to reshape it to a (3,4,2) array like below:

array([[[ 0,  1],
    [ 2,  3],
    [12, 13],
    [14, 15]],

   [[ 4,  5],
    [ 6,  7],
    [16, 17],
    [18, 19]],

   [[ 8,  9],
    [10, 11],
    [20, 21],
    [22, 23]]])

我尝试使用重塑,但是它给了我以下不是我想要的东西.

I've tried to use reshape but it gave me the following which is not what I want.

array([[[ 0,  1],
    [ 2,  3],
    [ 4,  5],
    [ 6,  7]],

   [[ 8,  9],
    [10, 11],
    [12, 13],
    [14, 15]],

   [[16, 17],
    [18, 19],
    [20, 21],
    [22, 23]]])

有人可以帮忙吗?

推荐答案

使用 transpose ,然后像这样-

reshape -

shp = x.shape
out = x.transpose(1,0,2,3).reshape(shp[1],-1,shp[-1])

这篇关于numpy:沿着指定的轴调整数组的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆