将numpy 3D数组重塑为2D [英] reshape numpy 3D array to 2D

查看:90
本文介绍了将numpy 3D数组重塑为2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数组,其形状为=(32,3,1e6)
我需要将其重塑为这个形状=(3,32e6)

I have a very big array with the shape = (32, 3, 1e6) I need to reshape it to this shape = (3, 32e6)

在摘要中,如何操作:

>>> m3_3_5
array([[[8, 4, 1, 0, 0],
        [6, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[8, 7, 1, 0, 3],
        [2, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[2, 4, 0, 2, 3],
        [2, 5, 5, 3, 2],
        [1, 1, 1, 1, 1]]])

为此::

>>> res3_15
array([[8, 4, 1, 0, 0, 8, 7, 1, 0, 3, 2, 4, 0, 2, 3],
       [6, 8, 5, 5, 2, 2, 8, 5, 5, 2, 2, 5, 5, 3, 2],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

我确实尝试了各种形状的组合没有成功:

I did try various combinations with reshape with no success::

>>> dd.T.reshape(3, 15)
array([[8, 8, 2, 6, 2, 2, 1, 1, 1, 4, 7, 4, 8, 8, 5],
       [1, 1, 1, 1, 1, 0, 5, 5, 5, 1, 1, 1, 0, 0, 2],
       [5, 5, 3, 1, 1, 1, 0, 3, 3, 2, 2, 2, 1, 1, 1]])

>>> dd.reshape(15, 3).T.reshape(3, 15)
array([[8, 0, 8, 2, 1, 8, 0, 8, 2, 1, 2, 2, 5, 2, 1],
       [4, 0, 5, 1, 1, 7, 3, 5, 1, 1, 4, 3, 5, 1, 1],
       [1, 6, 5, 1, 1, 1, 2, 5, 1, 1, 0, 2, 3, 1, 1]])


推荐答案

a.transpose([1,0,2])。reshape(3,15)会做你想要的。 (我基本上遵循@hpaulj的评论。)。

a.transpose([1,0,2]).reshape(3,15) will do what you want. (I am basically following comments by @hpaulj).

In [14]: a = np.array([[[8, 4, 1, 0, 0],
        [6, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[8, 7, 1, 0, 3],
        [2, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[2, 4, 0, 2, 3],
        [2, 5, 5, 3, 2],
        [1, 1, 1, 1, 1]]])

In [15]: a.transpose([1,0,2]).reshape(3,15)
Out[15]: 
array([[8, 4, 1, 0, 0, 8, 7, 1, 0, 3, 2, 4, 0, 2, 3],
       [6, 8, 5, 5, 2, 2, 8, 5, 5, 2, 2, 5, 5, 3, 2],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

这篇关于将numpy 3D数组重塑为2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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